1

我正在使用 jQuery 验证插件来验证表单数据。它在表单提交上工作正常。但是onkeyuponblur它不起作用。

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" ></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.0/jquery.validate.js"></script>

jQuery代码:

var validator = $("#myForm").validate( {

        rules:{
            attribute: "required",
            attributeValue: "required email"
        },
        messages:{
            attribute: "Required",
            attributeValue: {
                required: "Enter email",
                email: "Enter valid email"
            }
        }
    });

我在这里做错什么了吗?

4

4 回答 4

2
$("#element").blur(function(){
   $(this).validate({
      // your code here
   })
})

或更准确地说

$('#myForm').blur(function(){
    validator.validate()
});
于 2013-08-16T09:29:02.410 回答
1
Please include key up event or blur event in your validate function

var validator = $("#myForm").validate( {
    //this setting for on key up event
    onkeyup: function(element) { $(element).valid(); },
    rules:{
        attribute: "required",
        attributeValue: "required email"
    },
    messages:{
        attribute: "Required",
        attributeValue: {
            required: "Enter email",
            email: "Enter valid email"
        }
    }
});

if($("#myForm").valid()) {
    // call the function which you want to call after validation;
}
于 2013-08-17T18:37:51.507 回答
0

在 Validate 函数中为 onblur 添加以下代码段:

onfocusout: function (element) {
            $(element).valid();
        }

所以它会变成如下:

var validator = $("#myForm").validate( {

    rules:{
        attribute: "required",
        attributeValue: "required email"
    },
    onfocusout: function (element) {
        $(element).valid();
    },
     messages:{
        attribute: "Required",
        attributeValue: {
            required: "Enter email",
            email: "Enter valid email"
        }
    }
});

所以现在您将能够验证模糊的输入。

于 2015-11-15T14:18:00.727 回答
-2

您是否尝试在输入标签上添加类名?

<input type="text" class="require email" name="email"/>
于 2013-08-16T09:27:54.577 回答