$.validator.addMethod('username', function (value) {
return [A-Za-z0-9\-\_]+.test(value);
}, 'Username can only contain letters, numbers, underscores and dashes.');
I am no good with regular expressions OR jQuery/JavaScript which makes this almost impossible for me.
I simply want to add a method to validate a username to make sure it only has numbers, letters, underscores, and dashes (no spaces, periods, etc) but I can’t seem to get it right.
Any help would be appreciated.
There are two problems here. One, the regex needs delimiters. Two, the regex does not check whether the whole string is made up of those letters:
I also shortened the regex a bit (the character class is the same).