I'm using the jquery validation plugin to validate the input of a username.
$.validator.addMethod("alphanumeric", function(value, element) {
return this.optional(element) || /^[a-zA-Z0-9\-]+$/i.test(value);
}, "Your username can only contain letters, numbers and hyphens.<br />Spaces and special characters are not permitted.");
$('.setUsername-form').validate({
errorElement: 'label', //default input error message container
errorClass: 'help-inline', // default input error message class
focusInvalid: false, // do not focus the last invalid input
ignore: "",
rules: {
chosenUsername: {
required: true,
minlength: 4,
alphanumeric: true
}
},
stack overflow errors with "Your username can only contain....". This is good.
stackoverflow doesn't error - great!
stack-overflow errors with no output. This is an acceptable user name and should work.
Any ideas? I can't spot the issue. Is it the regex? I'm trying to allow alphabetical characters, numerical characters and hyphens. A-Z, a-z and -. Is [a-zA-Z0-9\-]
not correct?