I wish to check if a password contains at least one letter and a number. 开发者_StackOverflowspecial characters are accepted but not required...
That will be a simple password checker.
You can use lookahead assertions to check for existence of any digit and any letter as:
^(?=.*[a-zA-Z])(?=.*[0-9])
Using a single regex for this can lead to somewhat unreadable/unreliable code. It may make more sense to use simpler regexes eg [0-9]
to check for the existence of a digit and break the requirements of your password strength checker into a multi-line if
. Also this allows you to know more readily at what stage the validation failed and possibly make suggestions to the user.
([0-9].*[a-zA-Z])\|([a-zA-Z].*[0-9])
Not sure if the pipe needs to be escaped in your regexp environment or not.
Simple solution to check for password containing at least one letter, at least one digit and no spaces:
\S*(\S*([a-zA-Z]\S*[0-9])|([0-9]\S*[a-zA-Z]))\S*
/.*?(?:[a-z].*?[0-9]|[0-9].*?[a-z]).*?/
(just one possible solution)
This works for me
/^(\d+[a-zA-Z]|[a-zA-Z]+\d)(\d|[a-zA-Z])*/mg
精彩评论