jquery maxlength和minlength不工作?

时间:2014-04-30 10:42:06

标签: jquery jquery-validate

我正在使用jquery验证,但maxlengthminlength的验证无法正常运行。

function form_validate(){
    var validator=$("#cheque_pickup").validate({
    rules: {
        "pick[pin_code]": {
           required:true,
           digits:true,
           maxlength: 6,
           minlength:6
        }
    }
    });
}

<form onsubmit="form_validate();">
    <input type="text" name="pick[pin_code]" id="pin_code" placeholder="" value=""  />
</form> 

2 个答案:

答案 0 :(得分:0)

您不需要内联JavaScript来调用任何函数。该插件会自动捕获提交操作。事实上,您根本不需要form_validate()功能。这就是submitHandler回调选项已经为您做的事情。

$(document).ready(function() {
    $("#cheque_pickup").validate({
        rules: {
            "pick[pin_code]": {
                required: true,
                digits: true,
                // maxlength: 6,    // <- can use 'rangelength' instead
                // minlength: 6     // <- can use 'rangelength' instead
                rangelength: [6,6]  // <- one method will replace two
            }
        },
        submitHandler: function(form) {
            // your ajax or form submit here
            // alert('form is valid');
            return false;
        }
    });
});

<form id="cheque_pickup">
    <input type="text" name="pick[pin_code]" id="pin_code" placeholder="" value=""  />
</form>

工作演示:http://jsfiddle.net/RHHe5/

答案 1 :(得分:-1)

试试这个:

    $( document ).ready(function() {
       $("#cheque_pickup").validate({
        rules: {
            "pick[pin_code]": {
               required:true,
               digits:true,
               maxlength: 6,
               minlength:6
            }
        }
    });
    });

   function form_validate(){
    if($("#cheque_pickup").valid())
    {
        //post form here
    }

    }