0
/^(?=.*\d)(?=.*[!@&.$#]).{7,16}$/

It should allow between 7 and 16 characters and contain at least 1 numeric character and 1 special character and can't start with a number. I tried testing it but it does not work?

3
  • 1
    How come my password can't start with a number...
    – kennytm
    Commented Feb 17, 2010 at 19:14
  • @KennyTM: Who said this is used to test a password?
    – Gumbo
    Commented Feb 17, 2010 at 19:29
  • @Gumbo: Have you saw his/her previous question? (stackoverflow.com/questions/2282889/…)
    – kennytm
    Commented Feb 17, 2010 at 19:35

2 Answers 2

4

The only thing that I assume "does not work", which is a bit of a vague problem description to be honest, is the fact that it CAN start with a digit. Besides that, it works as you described.

Fix it like this:

/^(?=.*\d)(?=.*[!@&.$#])\D.{6,15}$/

A short explanation (in case you did not write the regex yourself):

^             # match the beginning of the input
(?=           # start positive look ahead
  .*          #   match any character except line breaks and repeat it zero or more times
  \d          #   match a digit: [0-9]
)             # end positive look ahead
(?=           # start positive look ahead
  .*          #   match any character except line breaks and repeat it zero or more times
  [!@&.$#]    #   match any character from the set {'!', '#', '$', '&', '.', '@'}
)             # end positive look ahead
\D            # match a non-digit: [^0-9]
.{6,15}       # match any character except line breaks and repeat it between 6 and 15 times
$             # match the end of the input
9
  • I tried the above with the password test11. and it it said it was invalid.
    – Xaisoft
    Commented Feb 17, 2010 at 19:34
  • @Xaisoft, the string test11. (including the DOT) works just fine.
    – Bart Kiers
    Commented Feb 17, 2010 at 19:41
  • must be a telerik issue. I test yours fine in another program and it worked fine.
    – Xaisoft
    Commented Feb 17, 2010 at 19:45
  • 1
    @Xaisoft, 6,16 way a mistake: it should've been 6,15 (not 7,16). The first character that'll be matched is a \D (non-digit) after which between 6 and 15 other characters should follow (1+6,1+15 == 7,16).
    – Bart Kiers
    Commented Feb 17, 2010 at 19:48
  • 1
    Xaisoft, the \D is in the beginning. The positive lookaheads don't actually consume any characters from the input. The first one says the string needs to have a digit somewhere, and the second lookahead says there must be one of those other characters somewhere. Once those two conditions are met, then the actual matching begins. The first character must be a non-digit, and then there must be six to 15 additional characters. The lookaheads from earlier ensure that we don't need to worry about what those characters really are. Commented Feb 17, 2010 at 20:10
2

The first two conditions are fulfilled but the third (must not start with a digit) is not. Because .* in ^(?=.*\d) does match when there is a digit at the first position.

Try this instead:

/^(?=\D+\d)(?=.*[!@&.$#]).{7,16}$/

Here \D (anything except a digit) ensures that that is at least one non-digit character at the start.

3
  • @Xaisoft: test11. works for me. What language/regular expression implementation do you use?
    – Gumbo
    Commented Feb 17, 2010 at 19:38
  • I tested it in an online Regular Expression tester and it does work. I am using it in Teleriks RadInputManager's RegExpTextBoxSetting. It is c# and asp.net in the background.
    – Xaisoft
    Commented Feb 17, 2010 at 19:41
  • I got it, apparently, there is no reason to add the / at the beginning and end when specifying the expression with telerik. It would have be nice if they specified this though.
    – Xaisoft
    Commented Feb 17, 2010 at 20:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.