0

I'm new to jquery and trying to use the validate a password of pretty basic complexity.

The password must be at least 7 characters, have at least 1 upper case, have at least 1 lower case, and at least 1 number OR special character.

This question is very similar to this answer I found, however I'm having trouble adding the "digit OR special char" part.

I think it's a regex I'm just not getting. My modification to that answer looks like this:

$.validator.addMethod("pwcheck", function(value) {
return /^[A-Za-z0-9\d=!\-@._*]*$/.test(value) // consists of only these
   && /[a-z]/.test(value) // has a lowercase letter
   && /[A-Z]/.test(value) //has an uppercase letter
   && (/\d/.test(value) || /~!@#$%^&*_-+=[]\{}|;':",.<> /.test(value)
       // has a digit or special char
});
Community
  • 1
  • 1
iaacp
  • 4,655
  • 12
  • 34
  • 39
  • You have a syntax error. Check your parenthesis. There is an open parenthesis on the digit test without a mating closer. This error should have appeared in your JavaScript console. – Sparky Oct 09 '15 at 16:41
  • Poster above me is a wizard. Beat me to the punch. – Richard Hamilton Oct 09 '15 at 16:43
  • With [your last edit](http://stackoverflow.com/revisions/33043286/2), you've completely changed the premise of the original question... this breaks any answers that have already been posted. I'm rolling it back. – Sparky Oct 09 '15 at 16:53
  • How? The problem was never that the code won't run, the problem was improving the regular expression. – iaacp Oct 09 '15 at 16:56
  • The actual posted code was completely broken by the syntax error and *"having trouble adding"* only affirms that. Both original answers interpreted the question the same. Your additional comments were needed in order to get what you really wanted. – Sparky Oct 09 '15 at 16:59
  • DEMO : https://jsfiddle.net/ssuryar/bjuhkt09/28/ – Surya R Praveen Apr 29 '20 at 09:03

2 Answers2

3

You needed to close your parenthesis on your last conditional. But I actually noticed a couple issues with your regex. For one, you need to group all the special characters in a bracket, so it won't match all those characters consecutively.

But beyond that, the Regex is way too verbose. Here's a much simpler solution.

$.validator.addMethod("pwcheck", function(value) {
    return /[A-Z]+[a-z]+[\d\W]+/.test(value)
});

This will match 1 or more uppercase characters, 1 or more lowercase characters and 1 or more digit/special character. There's one flaw with this however. It will only match in this particular order. That means the uppercase must come before the lowercase and the lowercase before the digits. It will match things like

AFDSabcd1435

but not things like

aA5fdHD14z

To have a more accurate validation, it should probably look more like this

$.validator.addMethod("pwcheck", function(value) {
    return /[A-Z]+/.test(value) && /[a-z]+/.test(value) && 
    /[\d\W]+/.test(value) && /\S{7,}/.test(value);
});

This isn't quite as clean as my previous idea, but it will pass more options.

Here's an example you can test out with a JavaScript function.

function testValue(string) { 
    return /[A-Z]+/.test(string) && /[a-z]+/.test(string) &&
    /[\d\W]/.test(string) && /\S{7,}/.test(string)
}

testValue("aGFbsdf4")
=> true
Richard Hamilton
  • 25,478
  • 10
  • 60
  • 87
  • Thank you as well :) Is there a better regular expression for any special character? Instead of listing them all as I did? – iaacp Oct 09 '15 at 16:49
  • Perfect, exactly what I was looking to improve. Thanks! – iaacp Oct 09 '15 at 16:59
  • Thanks for the improvements! The only issue I'm getting now is when I input something like `Tanooki!2#` - it looks like it's having trouble with combinations of numbers and symbols. – iaacp Oct 09 '15 at 17:18
2

EDIT: The following answer is for the original version of the OP.


Assuming your regex is correct, you have a syntax error.

This line...

&& (/\d/.test(value) || /~!@#$%^&*_-+=[]\{}|;':",.<> /.test(value)

should be...

&& /\d/.test(value) || /~!@#$%^&*_-+=[]\{}|;':",.<> /.test(value)

or

&& (/\d/.test(value) || /~!@#$%^&*_-+=[]\{}|;':",.<> /.test(value))
Community
  • 1
  • 1
Sparky
  • 98,165
  • 25
  • 199
  • 285