简体   繁体   中英

validation username and password

This is my php validtion code!

   <?php        //------------------------------------------------------------------------------>PHP VALIDATION
                $user="";
                $pass="";
                $nameErr="";
                $passErr="";
                //$pattern='/^[a-zA-Z0-9@_ ]*$/';
                if($_SERVER['REQUEST_METHOD']=='POST')
                {
                    if(empty($_POST['uname']))
                    {
                        $nameErr='Enter Your Name!';
                    }
                    else 
                    {
                        $user = test_input($_POST['uname']);
                        if(!preg_match('/^[a-zA-Z0-9@_]*$/',$user))
                            {
                                $nameErr=' Re-Enter Your Name! Format Inccorrect!( only alpha, numbers,@_ are allowed)';
                            }
                    }
                    if(empty($_POST['pas']))
                    {
                        $passErr='Enter Your Password!';
                    }
                    else
                    {
                        $user = test_input($_POST['pas']);
                        if(!preg_match('/^[a-zA-Z0-9@_]*$/',$pass))
                        {
                            $passErr='Invalid Format! Re-Enter Password!';
                        }
                    }   
                }



            function test_input($data)
            {
                 $data = trim($data);
                 $data = stripslashes($data);
                 $data = htmlspecialchars($data);
                 return $data;
            }
                ?>

HTML:
<html>
<body>
<div id='shw'><?php echo $user;?></div>
                <fieldset id='fld'>
                    <form method='post' name='f1' action='<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>'>
                        <table>
                            <tr>
                                <td>
                                    User Name:<input type='text' id='uname' name='uname' class='uname' placeholder='USERNAME' autocomplete='off' autofocus maxlength='25'><span class='error'>*</span>
                                    Password:<input type='password' id='pass' class='pass' name='pas' placeholder='PASSWORD' autocomplete='off'>
                                            <input type='submit' name='submit' id='loggin' value='LOGIN' class='login' onclick='val()'>
                                </td>
                            </tr>
                        </table>
                    </form>
                </fieldset>
            <div class='errormsg' id='errmsg'><?php echo $nameErr;?></div>
            </div>
</body>
</html>

iam having problm in validation i want to show user name after submit. problem is that my username if else is running good but it skips the password part. iwant that it shud also validate pass field then only shows the username! please help!

$user = test_input($_POST['pas']); should be $pass = test_input($_POST['pas']);

Your current code set $user in you first if..else block, it's not that PHP skips your second if...else block, it's just that no matter what this block will do, ûser is already set. If you don't want to display $user if password is invalid, just set $user later in you code: $nameErr = null; $passErr = null;

if (empty($_POST['uname'])) {
    $nameErr = 'Enter Your Name!';
} else {
    $userTest = test_input($_POST['uname']);

    if (!preg_match('/^[a-zA-Z0-9@_]*$/', $userTest)) {
        $nameErr = ' Re-Enter Your Name! Format Inccorrect! (only alpha, numbers, @_ are allowed)';
    } else {
        $valid++;
    }
}
if (empty($_POST['pas'])) {
    $passErr = 'Enter Your Password!';
} else {
    $pass = test_input($_POST['pas']);

    if (!preg_match('/^[a-zA-Z0-9@_]*$/', $pass)) {
        $passErr = 'Invalid Format! Re-Enter Password!';
    } else {
        $valid++;
    }
}

if (is_null($nameErr) && is_null($passErr)) {
    $user = $_POST['uname']
}

Here, I just check if $nameErr and $passErr are still null at the end of your tests. If both are null, then you can set $user, since no error has been detected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM