Cameron Cameron - 8 months ago 27
jQuery Question

Password regex validation always erroring

I have the following custom jQuery validation regex which should be making sure that passwords are at least 8 characters long, have a number and contain no spaces:

$.validator.addMethod("password", function(value, element) {
return this.optional(element) || /\A(?=.*\d)\S{8,}$/i.test(value);
}, "Password must have at least one digit, be 8 characters in length and not contain spaces.");

However not matter what I type into the box, it always calls the error. Is the regex incorrect?


In JS \A doesn't mean start of string, use ^ instead:


See MDN boundaries doc