jQuery验证插件未验证字段onblur和onkeyup

时间:2013-08-16 09:25:05

标签: jquery jquery-validate

我正在使用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 个答案:

答案 0 :(得分:2)

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

或更准确地说

$('#myForm').blur(function(){
    validator.validate()
});

答案 1 :(得分: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;
}

答案 2 :(得分: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"
        }
    }
});

现在,您将能够验证模糊输入。

答案 3 :(得分:-2)

您是否尝试在输入代码中添加类名?

<input type="text" class="require email" name="email"/>