Javascript regular expression password validation having special characters

后端 未结 7 711
闹比i
闹比i 2020-12-12 17:14

I am trying to validate the password using regular expression. The password is getting updated if we have all the characters as alphabets. Where am i going wrong ? is the re

7条回答
  •  [愿得一人]
    2020-12-12 18:03

    Use positive lookahead assertions:

    var regularExpression = /^(?=.*[0-9])(?=.*[!@#$%^&*])[a-zA-Z0-9!@#$%^&*]{6,16}$/;
    

    Without it, your current regex only matches that you have 6 to 16 valid characters, it doesn't validate that it has at least a number, and at least a special character. That's what the lookahead above is for.

    • (?=.*[0-9]) - Assert a string has at least one number;
    • (?=.*[!@#$%^&*]) - Assert a string has at least one special character.

提交回复
热议问题