-1

I'm trying to validate a password in my application by using Regex. I don't have much idea of Regex, This is what i got.

"^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$"

This expression is also checking Special Character, and 1 upper case character in password.

I need a Regex that full fills following criteria.

  1. Password Must be at least 8 characters Long.
  2. Password must contain 1 Letter
  3. Password must contain 1 Number
  4. Password should not contain any special Character.
dev90
  • 7,187
  • 15
  • 80
  • 153
  • 1
    The regex is taken from http://stackoverflow.com/questions/19605150/regex-for-password-must-be-contain-at-least-8-characters-least-1-number-and-bot, right? So, what have you tried to adapt it to your scenario? – Wiktor Stribiżew Oct 13 '16 at 22:01
  • What's wrong with special characters? – Jonnix Oct 13 '16 at 22:05
  • I don't need special characters in the text field. Is there any possibility we can add such conditions through regex? – dev90 Oct 13 '16 at 22:06
  • 2
    So take out `(?=.*?[#?!@$%^&*-])` but I don't know why you don't want to allow them, seems like a bad idea.. `(?=.*?[A-Z])` has a capital letter. `(?=.*?[a-z])` lowercase letter. `(?=.*?[0-9])` number. `.{8,}` over 8 characters or longer. There are sites that breakdown regexs, take a look at https://regex101.com/r/Bqu4bv/1 on the right side. – chris85 Oct 13 '16 at 22:06
  • Thanks @chris85, let me try that.. – dev90 Oct 13 '16 at 22:08
  • 1
    @chris85: You might want to edit [anubhava's original answer](http://stackoverflow.com/a/19605207/3832970) :) – Wiktor Stribiżew Oct 13 '16 at 22:08
  • Yup, its working fine, but i am very poor in understanding Regex – dev90 Oct 13 '16 at 22:11
  • 1
    @WiktorStribiżew Updated it. – chris85 Oct 13 '16 at 22:11
  • @Kirmani88 That's why the regex101 can be useful. It breaks down every part of the regular expression and explains it. – chris85 Oct 13 '16 at 22:13
  • Yes, I am looking into it. Thanks a lot for this reference site :) – dev90 Oct 13 '16 at 22:17

1 Answers1

1

I won't claim to be a regex master, but for the password criteria you describe, you could use:

/^(?=.*?[a-zA-Z])(?=.*\d)([a-zA-Z0-9])+$/ to test for conditions 2, 3, and 4.

For condition 1, I would just check the string length property to make sure it's at least 8 characters.

Note that in your current regex, you are requiring at least one uppercase letter, at least one lowercase letter, at least one digit, and at least one of the specified special characters (#?!@$%^&*-). Hope this helps.

Neil Strain
  • 586
  • 4
  • 4