Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

REGEX to validate password that can't contain some caracters

I need to check if a password match the following rules:

  • At least 8 characters (lenth)
  • One capital letter
  • One lower letter
  • One number
  • One special char
  • Can’t contain ‘.’ or ‘_’ (tricky part)

For example:

  • Bft$ns2E => should match
  • H2od%^.,3 => should’t match (notice the ‘.’)

I tried this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

^(?=.*?[A-Z])(?=(.*[a-z]){1,})(?=(.*[\d]){1,})(?=(.*[\W]){1,})(?!.*\s).{8,}$

That satisfy all rules, except the last one ( Can’t contain ‘.’ or ‘_’). Regex are always a pain for me and can’t figure out how to do this.

Thanks to all!

>Solution :

Your regex is on the right track. I would use:

^(?=.*?[A-Z])(?=.*[a-z])(?=.*\d)(?=.*\W)(?!.*[._]).{8,}$

This pattern says to:

^
    (?=.*?[A-Z])  assert capital letter
    (?=.*[a-z])   assert lowercase letter
    (?=.*\d)      assert digit
    (?=.*\W)      assert non word/special character
    (?!.*[._])    assert NO dot or underscore
    .{8,}         match a password of length 8 or greater
$
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading