9

I have the following password requirements:

1) Should be 6-15 characters in length
2) Should have atleast one lowercase character
3) Should have atleast one uppercase character
4) Should have atleast one number
5) Should have atleast one special character
6) Should not have spaces

Can anyone suggest me a RegEx for this requirement?

6
  • 4
    limiting the length of a password is rarely a good idea Commented Mar 6, 2010 at 21:06
  • Why does it have to be a regex? This would be more readable as ordinary code. Commented Mar 6, 2010 at 21:07
  • Mark - He's probably looking for a reg-ex so it can be used client and server side in a asp.net validator control. Commented Mar 6, 2010 at 21:09
  • @thekaido: Why is that a good idea? Commented Mar 6, 2010 at 21:20
  • @mark didnt say it was a good idea. just assuming why he wanted to use a reg ex. Also the built in .net membership provider has a web.config setting for password regex it can enforce, so that could another reason. Commented Mar 6, 2010 at 21:27

4 Answers 4

11

Not sure I would use a Regex for that : regex are not always the right tool for any possible kind of job...

Here, you specified a list of 6 requirements ; so, why not just use 6 different tests, one per requirement ?
Those 6 different tests, should I add, would be really simple -- while a Regex would be much harder to write (you asked for help -- you would probably not have for the 6 tests).

This would make your code a lot more easier to understand, I'd bet ;-)
And also : easier to maintain ; and easier to add/remove/change one of the condition corresponding to one of the requirements.

Sign up to request clarification or add additional context in comments.

3 Comments

The second advantage of this method would be providing better error messages when a password does not meet the requirements. For instance, indicating which required character type was omitted. There's nothing more frustrating than entering a password you think will work and getting a generic error message.
@thekaido : same for the 6 tests, even if the syntax might be a bit different ;;; @Emily : so true !
Wait wait! One tool doesn't work for all jobs? If that's so you're also saying I shouldn't be using a chainsaw for the fine decorative woodworking I'm about to do! Phew! I got lucky there!
10

I'm not entirely sure what you mean by "special character" so I am interpreting this to mean \W, but you can change this if you want:

^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*\W)\S{6,15}$ 

1 Comment

3

Regexlib.com has tons of examples for you and a searchable database of reg ex's.

Comments

1
1 => /^.{6,15}$/ 2 => /[a-z]/ 3 => /[A-Z]/ 4 => /\d/ 5 => /[#{special_chars_for_regex}]/ 6 => /^\S*$/ 

2 Comments

\W means not-word-character, not not-whitespace (that's \S, but you might as well incorporate that into the first condition like @Mark Byers did). Also, "special characters" just means punctuation.
I changed \W to \S (thanks). I didn't feel like spelling out the full set of special chars, but ~!@#$%^&*-_=+[{]}\|;:'",<.>/?() might do in a pinch.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.