1

I need one regex which validates below condition for password entered by user:

  1. it must have at least one capital letter
  2. it must have at least one number
  3. It must have at least one special character

I need regex to implement it in javascript.

I have tried this one

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

Thanks in Advance

Jerry
  • 70,495
  • 13
  • 100
  • 144
Piyush Hirpara
  • 1,279
  • 1
  • 11
  • 29
  • [What have you tried](http://mattgemmell.com/2008/12/08/what-have-you-tried/)? – Joseph Silber Sep 16 '13 at 15:00
  • just edited my question with what i have tried – Piyush Hirpara Sep 16 '13 at 15:04
  • Why do you have `(?=.*[a-z])`? Also you need to escape `[]` in a character class. – Jerry Sep 16 '13 at 15:05
  • 1
    Duplicate. This question gets asked a lot. I'm partial to my own answer(s). See: "[regex for password](http://stackoverflow.com/a/9611715/433790)" and "[Regular expression for a string that must contain minimum 14 characters, where at minimum 2 are numbers, and at minimum 6 are letters](http://stackoverflow.com/a/5527428/433790) – ridgerunner Sep 16 '13 at 15:29

1 Answers1

0

For the rules you mentioned:

/^(?=.*[0-9])(?=.*[A-Z])(?=.*[^a-zA-Z0-9])/;

This should suffice.

[^a-zA-Z0-9] matches anything that is not an alphabet and not a number, i.e. a special character.

I removed the case insensitive flag because you specifically mentioned there should be 1 uppercase letter.

Jerry
  • 70,495
  • 13
  • 100
  • 144