Niranjan Godbole
Niranjan Godbole

Reputation: 2175

Jquery validation not working for special characters

I am writing jquery validation to make a strong password. I wrote condition for special characters and it is not working. I am trying as below.

"regex": /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$])[0-9a-zA-Z]{8,}$/,
"alertText": "*Password should contain atleast one special character,one number and one upper case letter",

I am having trouble with (?=.*[!@#$]) part of code. If I remove this regular expression will work but it will not validate special characters. If I put (?=.*[!@#$]) nothing it will validate. Always I am getting an error popup. I am not sure about (?=.*[!@#$]) this part.

Upvotes: 3

Views: 997

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626845

Your consuming character class at the end of the pattern ([0-9a-zA-Z]) does not match the special chars that you require with the (?=.*[!@#$]) lookahead. Add those !@#$ chars there:

"regex": /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$])[0-9a-zA-Z!@#$]{8,}$/
                                                               ^^^^

Upvotes: 3

Related Questions