1

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?

1 Answer 1

2

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

/^(?=.*\d)\S{8,}$/i

See MDN boundaries doc

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.