Skip to content Skip to sidebar Skip to footer

Regex In Jquery Validation - Single Quote Issue In Jquery

I have a regular expression to validate a password to contain numbers, lowercase and uppercase characters. The one below works fine. But when I use this in jQuery Validation: txtPa

Solution 1:

There are several things that need to fixed in this regex:

  • In your lookahead checks for the password, you have a period without a quantifier. Hence it will always fail that check. For e.g., for the number check, you should have (?=.*\d) instead of (?=.\d).
  • You do not escape characters with a forward-slash /, instead you use a backslash \. You have it escaped incorrectly with a forwardslash in the last character class.
  • The only special characters or metacharacters inside a character class are the closing bracket (]), the backslash (\), the caret (^) and the hyphen (-). Hence it is not due to the single quote. The culprit is your caret which is not escaped.

Try this regex where the caret ^ is escaped instead of the quote ':

(?=^.{8,16}$)(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%\^&*()_+}{":;'?/>.<,])(?!.*\s)^.*$

I am assuming by your question, you meant a Regexp to validate a password with:

  • at least 1 number
  • at least 1 lowerchase character
  • at least 1 uppercase character
  • at least 1 special character out of the set !@#$%^&*()_+}{":;'?/>.<,
  • length between 8 and 16 characters

If this is true, please update your question with the clarified requirement.


Play with the regex in RegexPal


jQuery based code. Fiddle with this here:

var regex = /(?=^.{8,16}$)(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%\^&*()_+}{":;'?/>.<,])(?!.*\s)^.*$/g;
//if(regex.test($('#txtPassword').val())){ alert('Match'); }else{alert('No Match')}
if (regex.test('P@sSw0rD')) {
    alert('Match');
} else {
    alert('No Match');
}​

Post a Comment for "Regex In Jquery Validation - Single Quote Issue In Jquery"