skip to Main Content

I am using this pattern for password validation.

Validators.pattern('((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!"#$%&`{|}~()*+,-.\:/;<=>?@[\]^_/']).{10,})'),

But this validation is not working for ,;() etc but working for!@#$%^&

These characters are need to be accepted-
!"#$%&'()*+,-./:;<=>?@[]^_`{|}~

Can anybody suggest where the error is?

2

Answers


  1. Chosen as BEST ANSWER

    This pattern is working for me-

    Validators.pattern(/(?=.*d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\!"#$%&'()*+./:;<=>?@[]^_`{|}~,-]).{10,}/),
    

  2. If you have a list of special characters to match, you can create a string of escaped characters and use it to build your pattern dynamically:

    const specialChars = ',;:/\~!@#$%^&*()_+{}[]';
    const escapedChars = specialChars.replace(/[-[]{}()*+?.,\^$|#s]/g, '\$&');
    const pattern = new RegExp(`[${escapedChars}]`);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search