如何在输入字段jquery上添加验证错误?

时间:2015-05-31 19:12:08

标签: javascript php jquery

如何在输入字段本身上放置验证错误?因为我现在只显示div id =“result”中的错误。我只是想知道如何把它放在输入框上。

我刚刚使用jQuery Ajax代码,我的代码在jquery ajax语法中是否正确?

HTML

<form id="myForm" class="form-group" method="post" action="actions/forgot_password_action.php">
<div id="result"></div>
<table class="table-condensed" style="text-align:right">
    <tr>
        <td>Username:<span style="color:#FF0000;">*</span></td>
      <td><input type="text" name="username" class="form-control" id="username" autofocus="autofocus" placeholder="Username" autocomplete="off" size="40"></td>
    </tr>
    <tr>
        <td>New Password:<span style="color:#FF0000;">*</span></td>
        <td><input type="password" name="password" placeholder="New Password" id="password" class="form-control" data-toggle="tooltip" data-placement="top" title="Password must be atleast 8 characters"></td>
    </tr>
    <tr>
        <td>Confirm Password:<span style="color:#FF0000;">*</span></td>
        <td><input type="password" name="repassword" placeholder="Confirm Password" id="repassword" class="form-control" data-toggle="tooltip" data-placement="top" title="Please re-enter password"></td>
    </tr>
    <tr><td></td></tr><tr><td></td></tr>
    <tr>
        <td>Answer for security purposes<span style="color:#FF0000;">*</span></td>
        <td><input type="password" name="iqama" placeholder="What is your Iqama No.?" id="security" class="form-control"></td>
    </tr>

    <tr>    
        <td></td>
        <td align="right"><a href="index.php">Log in</a> <button id="submit" class="btn btn-primary">Submit</button></td>
    <tr>
</table>
</form>

的jQuery

$("button#submit").click(function(){

  $.post( $("#myForm").attr("action"),
    $("#myForm :input").serializeArray(),
    function(data) {
        $("div#result").html(data);
    });

$("#myForm").submit(function(){
     return false;
});

});

PHP

$username = $_POST['username'];
$password = $_POST['password'];
$repassword = $_POST['repassword'];
$iqama = $_POST['iqama'];

$check = mysqli_query($con, "SELECT * FROM users WHERE username = '".$username."'");
$get_lastname = mysqli_fetch_array($check);

$dbiqama = $get_lastname['iqama'];
$dbusername = $get_lastname['username'];

if(!empty($username) and !empty($password) and !empty($repassword) and !empty($iqama)){

    if($dbiqama == $iqama and $dbusername == $username){
        if($password == $repassword){

            if(strlen($password) > 7){
                $encrypt = sha1($password);
                mysqli_query($con, "UPDATE users SET password = '".$encrypt."' WHERE username = '".$username."'");
                echo "<script>alert('Try to Sign in now.!'); window.location.href='index.php';</script>";   
            }else{
                echo "<div class=panel-danger>";
                    echo "<div class=panel-heading>Password must be atleast 8 characters!</div>";       
                echo "</div>";  
            }   
        }else{
            echo "<div class=panel-danger>";
                echo "<div class=panel-heading>Password does not match!</div>";     
            echo "</div>";  
        }
    }else{
        echo "<div class=panel-danger>";
            echo "<div class=panel-heading>Invalid username or Iqama No.</div>";        
        echo "</div>";  
    }

}else{
    echo "<div class=panel-danger>";
        echo "<div class=panel-heading>Fill up all fields!</div>";      
    echo "</div>";  
}

0 个答案:

没有答案