JQuery验证奇怪的自定义方法错误

时间:2012-10-15 01:50:32

标签: jquery jquery-validate jquery-ui-datepicker

我有一个奇怪的错误,JQuery Validate和一个自定义方法。出于某种原因,每次更改字段时,validate方法会触发3-4次。它出于某种原因循环,我不明白为什么。验证器有效。唯一的问题是它不能与datepicker一起使用。我必须在datepicker中单击两次以获取要更改的输入中的值。似乎jquery validate正在劫持datepicker输入或其他东西。在这一点上我很无能为力,并会接受任何想法。

自定义方法:

$(document).ready(function () {

    $.validator.addMethod("laterDate", function(value, element) {   
       var id = "#" + element.id;
       var id_number = element.value.charAt(element.length - 1);
            alert(id_number);
       if(id_number == "1"){
            alert("in if")
            var id_value = $(id).fieldDate();
            var today = new Date();
                alert(id_value > today);
                return id_value > today;
      }
        else{
        alert("in else")
        var prev_id_number = String(parseInt(id_number) - 1);
        var prev_id = "#end_date_" + prev_id_number;
        var prev_id_value = $(prev_id).fieldDate();
        var id_value = $(id).fieldDate();
        return id_value > prev_id_value;
      }

    }, $.format("Enter a later date"));
});

验证码是:

$(document).ready(function () {
    $("#rate_new_new").validate({
        debug: true,

rules: {
                "rates[start_date_1]": {dateITA: true, maxlength: 50},
                "rates[end_date_1]": {dateITA: true, maxlength: 50, laterDate: true},
                "rates[price_1]": {testPrice: true}
                },
    messages: {

        "rates[start_date_1]": {
            dateITA: "Please enter a valid date",
            maxlength: "The name must be less than 50 characters"

        }
    },      

    submitHandler: function(form) {

                $(form).ajaxSubmit({

                        success: function(){
                          $('#lefttabs li:eq(4) a').tab('show');
                                            }
                                  });


                                  }
 });
 });

2 个答案:

答案 0 :(得分:3)

您使用的是JqueryUIDatePicker吗?

然后,试试这个:

$('#yourid').datepicker({
    onSelect: function(dateText, inst) {
        $(this).blur();
    }
});

答案 1 :(得分:0)

您的代码中有一些错误,例如缺少行结尾和String语句(我认为您的意思是.toString()),下面是您的固定代码:

$(document).ready(function() {

    $.validator.addMethod("laterDate", function(value, element) {
        var id = "#" + element.id;
        var id_number = element.value.charAt(element.length - 1);
        alert(id_number);
        if (id_number == "1") {
            alert("in if");
            var id_value = $(id).fieldDate();
            var today = new Date();
            alert(id_value > today);
            return id_value > today;
        }
        else {
            alert("in else");
            var prev_id_number = (parseInt(id_number) - 1).toString();
            var prev_id = "#end_date_" + prev_id_number;
            var prev_id_value = $(prev_id).fieldDate();
            var id_value = $(id).fieldDate();
            return id_value > prev_id_value;
        }

    }, $.format("Enter a later date"));
});​