PDA

View Full Version : Regular Expression for Username



prajwala
Mar 9th, 2007, 10:56 AM
We are currently using alpha-numeric validation. Will have to be changed to alpha-numeric or email.
Any characters that are valid in an email address need to be allowed for usernames. This includes at least ".", "_", and "@".
:)

chump2877
Mar 9th, 2007, 02:18 PM
We are currently using alpha-numeric validation. Will have to be changed to alpha-numeric or email.
Any characters that are valid in an email address need to be allowed for usernames. This includes at least ".", "_", and "@".
:)

Is there a question hidden in there somewhere, because I don;t understand what you are asking...

Philip M
Mar 9th, 2007, 05:01 PM
if (/[^A-z0-9\_\.\,\@]/gi.test(lstr.value) || (lstr.value=="")) { /// only alphas, digits, underline, period, comma or @ allowed

But surely a comma is not allowed in an email address.

chump2877
Mar 10th, 2007, 02:49 AM
my code is untested, but some corrections:

1) "g" modifier is unnecessary (as soon as you find a match, there's no need to continue searching)
2) correct ranges are A-Za-z for letters, and using this eliminates the need for using the "i" modifier
3) underscore, comma, and at symbol don;t need to be escaped
4) like you said, i've never seen a valid email address with a comma in it


var regex = /[^A-Za-z0-9_\[email protected]]/;
if (regex.test(lstr.value) || (lstr.value==""))
{
alert('no good');
}

Philip M
Mar 11th, 2007, 08:57 AM
1) "g" modifier is unnecessary (as soon as you find a match, there's no need to continue searching)

Agreed.

2) correct ranges are A-Za-z for letters, and using this eliminates the need for using the "i" modifier

Yes, but A-z is the same as A-Za-z

3) underscore, comma, and at symbol don;t need to be escaped

Yes, agreed, but still work correctly if escaped.