Not a big user of RegEx – never really understood them! However, I feel the best way to check input for a username field would be with one that only allows Letters (upper or lower), numbers and the _ character, and must start with a letter as per the site policy. The My RegEx and code is as such:
var theCheck = /[a-zA-Z]|\d|_$/g;
alert(theCheck.test(theUsername));
Despite trying with various combinations, everything is returning “true”.
Can anyone help?
Your regex is saying “does
theUsernamecontain a letter, digit, or end with underscore”.Try this instead:
This says “
theUsernamestarts with a letter and only contains letters, digits, or underscores”.Note: I don’t think you need the “g” here, that means “all matches”. We just want to test the whole string.