tags:

views:

69

answers:

2

Hi,

I'm trying to create a regular expression for a user name.

Here are the rules 1) At least 8 characters and at most 50 characters 2) Allows A-Z, a-z, numbers, and any other character except / character.

Thank you, -Tesh

+1  A: 

This should work for you...

[^/]{8,50}

If you want to be more specific about which characters you want to include then you can do something like this instead...

[A-Za-z0-9,\.!@#\$%\^&\*\(\)\-_\+\=]{8,50}

Ryan Berger
Inside character classes you only need to escape `\‍` and `]` (and depending on the position also `^` and `-`).
Gumbo
Thank you for the quick response. Been testing it, when I type in something like james/33333333. The regular expression returns true. If I enter something like james/33fffff it returns false. Been testing against [^/]{8,50}.
Hitesh Rana
@Hitesh Show some code and specify the language used. I imagine you want (need) to anchor the regex or otherwise have a small logic flaw.
pst
I created the following function in C# (.NET) public static Boolean ValidateUserName(string user_name) { Regex objRegEmail = new Regex("[^/]{8,50}"); return objRegEmail.IsMatch(user_name); }
Hitesh Rana
+2  A: 

Use

\A[^/]{8,50}\Z

or, in C#:

Regex regexObj = new Regex(@"\A[^/]{8,50}\Z");

The start-and-end-of-string anchors \A and \Z are necessary because otherwise regexObj.IsMatch() would return True even if only a part of the regex would match, and you want the string to match in its entirety.

Tim Pietzcker
Thanks, I tested it and it works. I appreciate your help.
Hitesh Rana