codingtcher.com
Theme Night
Home
Login

Password validation in php preg match

<?php
$password = "[email protected]";
if(preg_match('/^(?=.*\d)(?=.*[A-Za-z])[[email protected]#$%]{8,12}$/', $password)) {
    echo 'the password is valid';
} else{
    echo 'the password is invalid';
}
?>
1) of the string there has to be at least one number -> (?=.*\d)

2) and at least one letter -> (?=.*[A-Za-z])

3) and it has to be a number, a letter or one of the following: [email protected]#$% -> [[email protected]#$%]

4) and there have to be 8-12 characters -> {8,12}


<?php
$password = "[email protected]";
if(preg_match('/^(?=.*\d)(?=.*[@#\-_$%^&+=§!\?])(?=.*[a-z])(?=.*[A-Z])[[email protected]#\-_$%^&+=§!\?]{8,20}$/',$password)) {
    echo 'the password is valid';
} else{
    echo 'the password is invalid';
}
?>
1) at least one lowercase char

2) at least one uppercase char

3) at least one digit

4) at least one special sign of @#-_$%^&+=§!?

Related Code