Please Help me about preg match Validation
I need to validate a password it should have the following requirements:
- The password should have at least 8 characters
- The password s开发者_开发问答hould have at least 1 uppercase, 1 lowercase, 1 number, and 1 special character
- The password should have no continues character(ex. 12345 or abcd)
Please help me to do this.. any suggestions will be a big help. Thank you
Iterate string. If the character is uppercase then set bool isUppercase to true... If character is special character then set bool isSpecialCharacter to true. If the difference between this character and previous character is 1 then you have two consecutive characters, and you can stop iterating then (set bool haveConsecutiveCharacters to true).
The thing about consecutive characters is that if one of them is special character then they are not really consecutive (consider 'Z' and '[' that are next to each other in ASCII table).
After iterating check if all booleans are true and there are no consecutive characters.
If you really want a regexp for this, you'll have to use assertions :
/^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[\W\D\S]).{8,}$/
Now, the hard part is no consecutive characters. I suggest doing it with a loop instead of doing it with a regexp (actually, I don't know how to do it with a regexp).
精彩评论