1

I want to Add a validation for my password with special characters. My problem is when I use '%' it wont work. How can I add a validation for special characters properly?

$.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 a upper letter
      && /[!,%,&,@,#,$,^,*,?,_,~]/.test(value) // has a symbol
      && /\d/.test(value) // has a digit
});
j08691
  • 204,283
  • 31
  • 260
  • 272
  • @Mandy8055 Yes Thank you –  Jun 16 '20 at 03:21
  • @Mandy8055 how? Is there a better way? –  Jun 16 '20 at 03:27
  • @Mandy8055 yes you got it my requirement –  Jun 16 '20 at 03:32
  • 1
    You need to remove all those commas in `[!,%,&,@,#,$,^,*,?,_,~]` (`[!%&@#$^*?_~]`). If the character class is to contain a comma, 85 of them does no harm, but if not, a comma in the text will satisfy the requirement of that character class. – Cary Swoveland Jun 16 '20 at 03:43

1 Answers1

4

You can get through your requirement using a single regex alone. You may try the regex below:

^(?=\D\d)(?=[^A-Z]*[A-Z])(?=[^a-z]*[a-z])(?=[^-!@._*#%]*[-!@._*#%])[-A-Za-z0-9=!@._*#%]*$

Explanation of the above regex:

^, $ - Denotes the start and end of the line respectively.

(?=\D*\d) - Represents a positive look-around which asserts for at least a digit.

(?=[^A-Z]*[A-Z]) - Represents a positive look-around which asserts for at least an upper case letter.

(?=[^a-z]*[a-z]) - Represents a positive look-around which asserts for at least a lower case letter.

(?=[^-!@._*#%]*[-!@._*#%]) - Represents a positive look-around which asserts for at least a symbol among the listed. You can add more symbols according to your requirement.

[-A-Za-z0-9=!@._*#%]* - Matches zero or more among the listed characters. You can add more symbols accordingly.

You can find the demo of the above regex in here.

Sample implementation of the above regex in javascript:

const myRegexp = /^(?=[^\d\n]*\d)(?=[^A-Z\n]*[A-Z])(?=[^a-z\n]*[a-z])(?=[^-!@._*#%\n]*[-!@._*#%])[-A-Za-z0-9=!@._*#%]*$/gm; // Using \n for demo example. In real time no requirement of the same.
const myString = `thisisSOSmepassword#
T#!sIsS0om3%Password
thisisSOSmepassword12
thisissommepassword12#
THISISSOMEPASSWORD12#
thisisSOMEVALIDP@SSWord123
`;
// 1. doesn't contain a digit --> fail
// 3. doesn't contain a symbol --> fail
// 4. doesn't contain an Upper case letter --> fail
// 5. doesn't contain a lowercase letter --> fail
let match;
// Taken the below variable to store the result. You can use if-else clause if you just want to check validity i.e. valid or invalid.
let resultString = "";
match = myRegexp.exec(myString);
while (match != null) {
  resultString = resultString.concat(match[0] + "\n");
  match = myRegexp.exec(myString);
}
console.log(resultString);

References:

  1. Recommended Reading: Principle of Contrast.
  • 1
    A better idea is using the [principle of contrast](https://www.rexegg.com/regex-style.html#contrast) in such regexps, use `/^(?=\D*\d)(?=[^A-Z]*[A-Z])(?=[^a-z]*[a-z])(?=[^-!@._*#%]*[-!@._*#%])[-A-Za-z0-9=!@._*#%]*$/` – Wiktor Stribiżew Jun 17 '20 at 17:13
  • Your demo is wrong, [here is the right one](https://regex101.com/r/GqyA2Z/6). You are not testing against separate strings at regex101, you are testing against a single multiline string there. It is not what you have in real life, so you need to exclude newlines from the negated character classes. You can trick people into believing weird things at regex101. Use it wisely with care. – Wiktor Stribiżew Jun 17 '20 at 17:28
  • I mean to say your "But it is not working as it is supposed to" is wrong: my top comment regex is just fine as is in the real code with real life string input. – Wiktor Stribiżew Jun 17 '20 at 17:44
  • 1
    I did not get offended. Just read the `line-breaks` paragraph in my [Bonus: My regex works at regex101.com, but not in...](https://stackoverflow.com/a/39636208/3832970) anwer. – Wiktor Stribiżew Jun 17 '20 at 17:49
  • Thanks to @WiktorStribiżew for suggesting the enhancement in the answer. –  Jun 18 '20 at 11:39