0

I'm working on a addition method for the jquery validate plugin which must detect if 3 or more consecutive identical characters are entered, and prompt a message. To do so I need to negate a regex expression. This is my method:

$.validator.addMethod("pwcheckconsecchars", function (value) {
    return /(.)\1\1/.test(value) // does not contain 3 consecutive identical chars
}, "The password must not contain 3 consecutive identical characters");

The code above shows the error message when no consecutive identical characters have been entered, so I need to negate the expression /(.)\1\1/ so that it only appears when the error has been made.

This jsfiddle shows my code so far: http://jsfiddle.net/jj50u5nd/3/

Thanks

Sparky
  • 98,165
  • 25
  • 199
  • 285
Adorablepolak
  • 157
  • 4
  • 16
  • 5
    How about `return !/(.)\1\1/.test(value);` – Pointy Feb 09 '15 at 17:47
  • Regarding your jsFiddle, you should really combine all of those various regular expressions into one. There is no need to have FIVE custom methods! You could even incorporate the min/max length rules into it replacing seven rules at once. – Sparky Feb 09 '15 at 17:50
  • @Sparky. The validation rules were set based on business requirements that I can't modify. The thing is that I'd need to show a custom message for each rule not passed. Would that be possible with just a method that gathers all rules? – Adorablepolak Feb 09 '15 at 18:15
  • @Pointy. That's not working. Any other suggestion on how to fix this? – Adorablepolak Feb 09 '15 at 18:16
  • Enter "AAAa1" and it's working fine for me: http://jsfiddle.net/jj50u5nd/6/ – Sparky Feb 09 '15 at 18:22

1 Answers1

1

As stated by Pointy in the comments, use a ! character to negate or invert the result.

$.validator.addMethod("pwcheckconsecchars", function (value) {
    return !/(.)\1\1/.test(value); // does not contain 3 consecutive identical chars
}, "The password must not contain 3 consecutive identical characters");

Enter a "AAAa1" into the demo below, it satisfies enough of the other rules to demonstrate this particular method is working.

DEMO: http://jsfiddle.net/jj50u5nd/6/

Sparky
  • 98,165
  • 25
  • 199
  • 285