/^(?=.*\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?
/^(?=.*\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?
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
test11.
(including the DOT) works just fine.
Commented
Feb 17, 2010 at 19:41
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
).
Commented
Feb 17, 2010 at 19:48
\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
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.
test11.
works for me. What language/regular expression implementation do you use?