-4

Regex for password of minimum length-7 without special characters , atleast one uppercase and one number .

In my case, regex which satisfies:

Killer1 - atleast one uppercase (K), atleast one number (1) , minumum length - 7

Melbourne123- valid

London24 - valid

Thanks in advance.

kaarthick raman
  • 793
  • 2
  • 13
  • 41
  • Pure code-writing requests are off-topic on Stack Overflow -- we expect questions here to relate to *specific* programming problems -- but we will happily help you write it yourself! Tell us [what you've tried](http://stackoverflow.com/help/how-to-ask), and where you are stuck. This will also help us answer your question better. – Thomas Ayoub Jul 20 '16 at 13:14
  • `^(?=\w*([A-Z]\w*[0-9]|[0-9]\w*[A-Z])\w*).{7,}$` – logi-kal Jul 20 '16 at 13:28
  • Hi thomas @ThomasAyoub i am very new to web development. I understand you point.But i had no other go to find how a regex can be built for a condition so i posted. – kaarthick raman Jul 21 '16 at 04:22
  • Thanks for ur response.@horcrux – kaarthick raman Jul 21 '16 at 04:23

1 Answers1

5

Minimum length 7

This part is unsurprisingly the simplest. You can just use:

.{7,}

In order to perform the other checks in a single regex, you need to make use of look-aheads as follows:

at least one upper-case

(?=.*[A-Z])

at least one number

(?=.*\d)

without special characters

I would strongly advise against this requirement if at all possible. Adding this does not improve your security, and will only frustrate your users. But, if you really must, then:

(?!.*[^a-zA-Z0-9])

(Modify the above as appropriate -- depending on what exactly you mean by "special" characters.)

Putting this all together into a single pattern, the final answer is:

\A(?=.*[A-Z])(?=.*\d)(?!.*[^a-zA-Z0-9]).{7,}

You could also simplify this regex slightly, by merging the "no special chars" and "minimum length" requirements into a single regex condition as follows:

\A(?=.*[A-Z])(?=.*\d)[a-zA-Z0-9]{7,}\z

(Note the additional use of a \z anchor here, in order to check that all password characters are in the whitelisted "non-special" characters.

Tom Lord
  • 27,404
  • 4
  • 50
  • 77