Reputation: 41
I want regular expression for password in php that must match following conditions :
eg. adfjs345, 454dkfj, kfj45kj45, 870jdfk56fdjk are valid input.
Upvotes: 1
Views: 105
Reputation: 26153
^(?=.*\d)(?=.*[A-Za-z])[A-Za-z\d]
(?=.*\d) - string contains digit
(?=.*[A-Za-z]) - string contains letter
^[A-Za-z\d] - string starts with letter or number
Upvotes: 0
Reputation: 943999
Keep it simple. Use two regular expressions.
if( preg_match("[a-zA-Z]", $password) && preg_match("[0-9]", $password) ) {
all_ok();
}
Upvotes: 1
Reputation: 798
1- password must contain at least one letter and one digit
/^(?:[0-9]+[a-z]|[a-z]+[0-9])[a-z0-9]*$/i
2- password start with letter or number
^[a-zA-z0-9]+
Upvotes: 1