Validating for at least one upper case one lower case or special character present in a string

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 
Syntax
preg_match( $pattern,$subject,&$matches = null, $flags = 0, $offset = 0 )
$pattern : The pattern to search for
$subject : Input string
$matches : If matches is provided, then it is filled with the results of search
$flags : Flags
$offset : The search to start in bytes

Returns 1 if the pattern matches , 0 if it does not, or false on failure.

Check for at least 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

$string='rtAF 78a';
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 
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.
String Functions Check for all upper case letters or all lower case letters. Coverting string to all Upper case letters or Lower case
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Post your comments , suggestion , error , requirements etc here





    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer