Jquery验证:错误消息始终显示多次

时间:2012-10-14 15:32:29

标签: jquery jquery-plugins

我正在尝试使用jquery验证进行验证,并将错误消息显示在div容器中。我的问题是,当我在输入字段中键入内容时,错误消息始终显示在" div.info"但我只想在提交表单时始终显示错误消息。我的下一个问题是,当我提交不止一次的错误消息时,也会多次显示错误消息?

我的代码如下所示

Html代码

<div class="info"></div>
 <form id="fmlogin">
  <fieldset>
    <label>Email Address</label>
    <input type="text" name="email" />
    <label>Password</label>
    <input type="password" name="password"/>
    <input type="submit" value="login" />
  </fieldset>
 </form>

jquery

<script type="text/javascript">
        $(document).ready(function(){       
        $("form#fmlogin").submit(function() {
            data= $("form#fmlogin").serialize();
            return false;
        });

        $("#fmlogin").validate({
            rules: {
                email: {
                    required: true,
                    email:true
                },
                password: {
                    required: true,
                }
            },      
        messages: {
            email: {
                required: "Email Address is required",
                email:"Email Address is not Valid"
            },
            password: {
                required: "Password is required",
            }
          },
        errorElement: "div",
        wrapper: "div", 
        errorPlacement: function(error, element) {  
            error.appendTo($('.info'));
        },
        submitHandler: function(form){          
            $.ajax({
                type: "POST",
                url: "login.php",
                data:data,
                success: function(response){
                    $(".info").text(response);
                }   
            }); 
        }
        }); 
        });
</script>   

我是jquery验证的新手,请帮帮忙?

谢谢

1 个答案:

答案 0 :(得分:0)

fmlogin

使用此

  var data=$("#fmlogin").serialize();
 $("#fmlogin").submit(function(){
   $.ajax({
            type: "POST",
            url: "login.php",
            data:data,
            success: function(response){
      //if you want to show error more that once than use append instead of text
                $(".info").append(response);
            }   
    })