Password Validation Function

Written by @kerixa 29 August 2020

Validating passwords is a need whenever there is a panel and users most login. Therefore, you can use the following password validation function to ensure that the length of the selected password is more than 6, and it contains one upper case and one special characters. Password Structure Validation: $passVer = the password If there is an error with invalid symbols it will return the details of that error in a bullited list If the password does not meet the spefications it will return an ordered list

    with the description of why it did not meet the specifications.

    Code Snippet:

                                                    
                                                    <!-- this script is provided by https://www.phpfreecode.com coded by: Kerixa Inc. -->
    <?
    $passVer    = $_POST['passVer'];
    function checkPassword($passVer){
        global $symbolErrors;
        global $results;
        $invalid_symbols = array(0 => "$",1 => "%",2 => "&");
        $valid_symbols = array(0 => "!",1 => "@",2 => "#",3 => "^",4 => "*",5 => "(",6 => ")",7 => "-",
                        8 => "_",9 => "=",10 => "+",11 => "[",12 => "]",13 => "{",14 => "}",15 => ";",
                        16 => ":",17 => ",",18 => "<",19 => ">",20 => ".",21 => "/",22 => "~");
        $length = strlen($passVer);    
        if ($length >= 6){
            $lengthVerified = true;
        } else {
            $lengthVerified = false;
        }
         
        for($i=0;$i<$length;$i++){
            $chars[] = substr($passVer, $i, 1);
        }
        $symbolErrors = "<ul>";
            while(list($key,$val) = each($chars)){
                if (in_array($val,$invalid_symbols)){
                    $symbolErrors .= "<li><span class='defaultBoldRed'>$val</span> is an invalid symbol.</li>";
                    $invalidSymbol = true;
                }else{
                    $invalidSymbol  = false;
                }
                
                if (in_array($val,$valid_symbols)){
                    $validSymbol = true;
                }else{
                    $validSymbol = false;
                }
                
                if (ereg('[0-9]', $val)){
                    $numericVerified = true;
                }else{
                    $numericVerified = false;
                }
                
                if(ereg('[A-Z]', $val)){
                    $alphaUpVerified = true;
                }else{
                    $alphaUpVerified = false;
                }
                
                if(ereg('[a-z]', $val)){
                    $alphaLowVerified = true;
                }else{
                    $alphaLowVerified = false;
                }
            }
        $symbolErrors .= "</ul>";
         
        $results = "<ol>";
            if ($lengthVerified == true){
                //$results .= "<span class='defaultGreen'>The length check passed</span><br>";
            } else {
                $results .= "<li><span class='defaultRed'>The length check failed, your password needs to be 6 characters or more.</span><br></li>";
            }
             
            if ($invalidSymbol != true){
                //$results .= "<span class='defaultGreen'>The invalid symbol check passed</span><br>";
            } else {
                $results .= "<li><span class='defaultRed'>The invalid symbol check failed and are mentioned above.</span><br></li>";
            }
             
            if ($validSymbol == true){
                //$results .= "<span class='defaultGreen'>The valid symbol check passed</span><br>";
            } else {
                $results .= "<li><span class='defaultRed'>The valid symbol check failed. You need to have a symbol in your password other than $ % or &amp;.</span><br></li>";
            }
             
            if ($numericVerified == true){
                //$results .= "<span class='defaultGreen'>The numeric check passed</span><br>";
            } else {
                $results .= "<li><span class='defaultRed'>The numeric check failed because you need to have a number in your password.</span><br></li>";
            }
             
            if ($alphaUpVerified == true){
                //$results .= "<span class='defaultGreen'>The alpha upper case check passed</span><br>";
            } else {
                $results .= "<li><span class='defaultRed'>The alpha upper case check failed because you need to have one of the letters capitalized.</span><br></li>";
            }
             
            if ($alphaLowVerified == true){
                //$results .= "<span class='defaultGreen'>The alpha lower case check passed</span><br>";
            } else {
                $results .= "<li><span class='defaultRed'>The alpha lower case check failed because you need a lower case letter.</span><br></li>";
            }
        $results .= "</ol>";
    }
     
    echo"<html>
    <head>
        <title>Change Password</title>
        <style>
            .default{font:9pt Arial,Helvetica,Veranda;color:#000000;}
            .defaultWhite{font:9pt Arial,Helvetica,Veranda;color:#FFFFFF;}
            .defaultRed{font:9pt Arial,Helvetica,Veranda;color:#D60000;}
            .defaultBlack{font:9pt Arial,Helvetica,Veranda;color:#000000;}
            .defaultBlue{font:9pt Arial,Helvetica,Veranda;color:#00529C;}
            .defaultGreen{font:9pt Arial,Helvetica,Veranda;color:#006600;}
            .defaultOrange{font:9pt Arial,Helvetica,Veranda;color:#DC9306;}
            .defaultBold{font:bold 9pt Arial,Helvetica,Veranda;color:#000000;}
            .defaultBoldWhite{font:bold 9pt Arial,Helvetica,Veranda;color:#FFFFFF;}
            .defaultBoldBlack{font:bold 9pt Arial,Helvetica,Veranda;color:#000000;}
            .defaultBoldYellow{font:bold 9pt Arial,Helvetica,Veranda;color:#D60000;}
            .defaultBoldBlue{font:bold 9pt Arial,Helvetica,Veranda;color:#00529C;}
            .defaultBoldLtBlue{font:bold 9pt Arial,Helvetica,Veranda;color:#EDEFF3;}
            .defaultBoldRed{font:bold 9pt Arial,Helvetica,Veranda;color:#D60000;}
            .defaultBoldGreen{font:bold 9pt Arial,Helvetica,Veranda;color:#006600;}
            .defaultBoldOrange{font:bold 9pt Arial,Helvetica,Veranda;color:#DC9306;}
            .defaultBoldGrey{font:bold 9pt Arial,Helvetica,Veranda;color:#888888;}
        </style>
    </head>
     
    <body>
    <form method='post'><P>&nbsp;</P>
        <table border='0' cellpadding='1' cellspacing='0' width='400' align='center' bgcolor='#000000'><tr><td>
            <table border='0' cellpadding='2' cellspacing='0' width='400' align='center' bgcolor='#FFFFFF'>
                <tr class='highlight'>
                    <td align='center' class='defaultBoldWhite' bgcolor='#00529C'>Please change your password in the box below.</td>
                </tr>
                <tr class='noHighlight'>
                    <td align='center'>
                        <input type='text' name='passVer' value='$passVer' class='defaultBlue'>
                        <input type='submit' value='CHANGE' class='defaultBoldBlue'>
                    </td>
                </tr>
                <tr class='highlight'>
                    <td class='defaultBold'>
                    <P align='center'>Below is the description of the problem with your password.</P>
                    ";
                    if (isset($passVer)){
                        checkPassword($passVer);
                        if ($symbolErrors != "<ul></ul>" || $results != "<ol></ol>"){
                            echo $symbolErrors;
                            echo $results;
                        } else {
                            //YOUR UPDATE STATEMENT HERE
                            echo "<center><span class='defaultBoldRed'>Your password acceptable, <a href='#'>Click Here</a> to continue.</span></center>";
                        }            
                    }
                     
                    echo"
                    </td>
                </tr>
            </table>
        </td></tr></table>
    </form>
    </body>
    </html>";
    ?><a target='_blank' href='https://www.phpfreecode.com' style='font-size: 8pt; text-decoration: none'>Php Best Codes</a>                                                
                                                

    Example:


    About @kerixa

    I am Krishna Eydat. I studied Software Engineering at University of Waterloo in Canada. I lead a few tech companies. I am passionate about the way we are connected. I would like to be part of something big or be the big deal!

    K

    Comments


    Here you can leave us commments. Let us know what you think about this code tutorial!

    0 / 300

TRENDING POST
1
2
3
4
5
VISITORS
Online Users: 12
Recent Members: grkkid, Manaakividuinfo.com, karticksv, sava, tinatina
advertisement 2