1

I have to do jQuery form validation for password.

The password should contain at least two special characters in any order. I have tried with Regular Expression for password validation but it does not address that two random special characters can come at any order.

How do I do it using a JavaScript regular expression?

Community
  • 1
  • 1
Nasif Md. Tanjim
  • 3,862
  • 4
  • 28
  • 38

3 Answers3

6

You do not have to use look-arounds in cases when you do not have to.

If you only need to make sure the string has at least 2 characters of a specific set, use this kind of a regex (with a negated class to make it more robust):

/(?:[^`!@#$%^&*\-_=+'\/.,]*[`!@#$%^&*\-_=+'\/.,]){2}/

See demo

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0

In javascript it worked for me:

/(?=(.*[`!@#$%\^&*\-_=\+'/\.,]){2})/
Nasif Md. Tanjim
  • 3,862
  • 4
  • 28
  • 38
0
var goodtogo = false;
var pass = 'simp!le@';   //example
var times = pass.match(/[\\\[\]\/\(\)\+\*\?`!@#$%\^&_=-]/g).length;
if(times >= 2)
    goodtogo = true;

Now I advice you to try several passwords and if you find a bug or something don't hesitate to yell back.

And if you have more special chars just add them to the parameter for match.

Hope it helps.

Scaramouche
  • 3,188
  • 2
  • 20
  • 46