0

I have a username and password input field and I'm using the jquery validation plugin to validate my form. I know how to create custom rules and all but I'm just wondering how to write the regexp for it.

I want the password to be:
1) minimum 8 characters (this is taken care of by minlength in rules)
2) Contain a symbol (!@#$%^&*?~`'")
3) Contain a number (0-9)

jQuery.validator.addMethod("password", function(value, element) { 
        return this.optional(element) || /.../i.test(value);
    }, jQuery.format("Enter correct password")); 

I just don't know how to write the regex to search the input and see if it contains at least 1 symbol and 1 number. Any ideas? Thanks

1 Answer 1

4

Here we go

^(?=.*?[@#$%^&*?~`])(?=.*?[0-9]).{8,}$

It tells, starting from the beginning of the string, that any string with length >=8 is ok

.{8,}

You need also to tell that from the beginning of the string you need a symbol in any position

^(?=.*?[@#$%^&*?~`])

And a digit in any position

(?=.*?[0-9])

using positive lookahead

A working example

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.