I have regular expression which tests following rules,
- Password must have at-least one char.
- Password must have at-least one numeric.
- Password must have at-least one alphanumeric char.(More then one should be allowed)
My attempt is this,
/^([a-zA-Z+]+[0-9+]+[!@#$%^&*])$/
This works fine as for most cases except if I add more then one alpha-numeric chars.
Tests
- Test [qwer1234] Result [Not Valid] Conclusion [Passed]
- Test [qwer1234$] Result [Valid] Conclusion [Passed]
- Test [qwer1234#$] Result [Not Valid] Conclusion [Failed]
The last test should get Passed but it fails. I know where things are wrong but couldn’t get hang of the regex magic. My thoughts about what is wrong is,
[0-9+] // This + sign shows that you can have more then 1 of that range of numerics
Where,
[!@#$%^&*] // Does not have the + sign
I tried,
[!@#$%^&*+] // Does not have the + sign
[!@#$%^&*]+ // Does not have the + sign
Both didn’t work. What am I missing?
Depending on what you actually need, from your confusing description of your situation, here are two regexes that I hope help. This first one requires that the password start with at least 1 Alphabetic character, have at least 1 Numeric character second, then at least 1 Special character last.
http://jsfiddle.net/ECwP8/
If you don’t require the characters to come in a specific order, you can try this regex:
http://jsfiddle.net/bJTTk/1/
This one simply requires that there be at least 1 Alphabetic character, at least 1 Numeric character, and at least 1 Special character, occur in the password in any order.