Cameron Cameron - 1 year ago 81
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?

Answer Source

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


See MDN boundaries doc

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download