Validating for at least one upper case one lower case or special character present in a string
We have seen how to check for all the upper case letters or all lower case letters ( or special chars ) presence in a string variable. However we may need a situation where we have to ask the user (while entering password ) to enter at least one lower case letter or one upper case letter or one special characters.
We will check one by one first and then develop a script to combine all these points.
This code will be useful for checking at server side ( not client side ) password entered by user.
Check for at least one upper case character
We used preg_match which returns True of False after checking the string.
$string='rtAF78a';
if(preg_match('/[A-Z]/', $string)){
echo " There is at least one Upper Case Characters inside the string ";
}else{
echo " There is no Upper case characters inside the string ";
}
The output is
There is at least one Upper Case Characters inside the string
Check for at lease one lower case character
if(preg_match('/[a-z]/', $string)){
echo " There is at least one lower Case Characters inside the string ";
}else{
echo " There is no lower case characters inside ";
}
Output is
There is at least one lower Case Characters inside the string
Checking for presence of blank space inside string
We will use string search function strstr() to search for presence of blank string inside.
$string='rtAF 78a';
if (strstr($string,' ')) {
echo " There is at least one blank space present inside the string ";
}else{
echo " There is NO blank space present inside the string ";
}
Output is
There is blank space present inside the string
Check for special characters
if (preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬-]/', $string)) {
echo " There is at least one special characters present inside the string";
}else{
echo " There is no special characters present in the string ";
}
There is no special characters present in the string