Sudip977
Sudip977

Reputation: 41

Regular expression for password in php

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

Answers (3)

splash58
splash58

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

demo

Upvotes: 0

Quentin
Quentin

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

Fahmi B.
Fahmi B.

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

Related Questions