Forum Moderators: open
I have tried the following code and I'm sure that it's my logic that's incorrect:
var myRegxp = /([a-zA-Z0-9_-]+)$/;
if(myRegxp.test("a$n")==false)
{
alert( "Please enter a valid username." );
}
It will return false if a completely non-alphanumeric string is entered (e.g. '%'), but return positive if an alphanumeric string containing non-alphanumeric characters is entered (e.g. 'abc%' or 'abc%123').
Can anyone point out what's wrong?
Thanks in advance. :)
**** Edit ****
I tkink I fixed it by adding a caret at the beginning:
var myRegxp = /^([a-zA-Z0-9_-]+)$/;
if(myRegxp.test("a$n")==false)
{
alert( "Please enter a valid username." );
}
I'm surprised it took so long and I didn't find a code snippet like this on the net :o