startdate验证后的文本输入datepicker enddate

时间:2012-09-12 11:22:20

标签: validation jquery jquery-plugins jquery-ui-datepicker

我有2个输入框,startdate和enddate。我试图强制用户在第一个输入框中的日期之后的第二个输入框中选择日期。这将在ajax save上验证。

包括“无效”在内的所有内容都很有效,但在FF或歌剧中不起作用。 在FF和歌剧中,无论开始日期之前或之后的日期是什么,它只是继续显示isValid:“必须在开始日期之后”消息并且不验证。

是否有任何想法为什么,或使用datepickers的解决方案,以便它修改第二个datepicker上的minDate。

以下是ajax模式的按钮:

buttons: {
          'Save': function() {
                if($("#new_request_form").valid()){
                // do stuff if form validates
                submitFormWithAjax($("#new_request_form"));    
                $(this).dialog('close');
                } else {
                    // do stuff if form does not validate
                }

                },
          'Cancel': function() {$(this).dialog('close');}
        },

这是日期检查,以确保enddate在startdate之后:

    jQuery.validator.addMethod("isValid", function (value, element) {
        var startDate = $('#startdate').val();
        var finDate = $('#enddate').val();
        return Date.parse(startDate) < Date.parse(finDate);
    }, "* End date must be after start date");  

    $("#new_request_form").validate({
         rules: {
            days: "required",
            hours: {
                    required: true,
                    number: true,
                    digits: false  
            },         
            startdate: "required",
            enddate: {
                    required: true,
                    isValid: true
            }         
        },
        messages:{
            days: "Required",
            hours: {
                    required: "Required",
                    number: "Numbers Only"
            },   
            startdate: "Required",
            enddate: {
                    required: "Required",
                    isValid: "Must be after start date"
            }  
        }
    });


    var $ac_start_date = '<?php echo $ac_end_date ?>',
        $ac_start_date_flip = '<?php echo $ac_end_date_flip ?>',
        $ac_start_parsed = Date.parse($ac_start_date),
        _today = new Date().getTime();

    // For Opera and older winXP IE n such
    if (isNaN($ac_start_parsed)) { 
        $ac_start_parsed  = Date.parse($ac_start_date_flip);
    }

    var _aDayinMS = 1000 * 60 * 60 * 24; 

    // Calculate the difference in milliseconds
    var difference_ms = Math.abs($ac_start_parsed - _today);

    // Convert back to days and return
    var DAY_DIFFERENCE = Math.round(difference_ms/_aDayinMS);       

    // do initialization here
    $("#startdate").datepicker({
                dateFormat: 'dd-mm-yy',
                changeMonth: true,
                changeYear: true,
                yearRange: '0:+100',
                minDate: '+1d',         
                maxDate: '+' + (DAY_DIFFERENCE + 1) + 'd'
    });

    // do initialization here
    $("#enddate").datepicker({
                dateFormat: 'dd-mm-yy',
                changeMonth: true,
                changeYear: true,
                yearRange: '0:+100',
                minDate: '+1d',         
                maxDate: '+' + (DAY_DIFFERENCE + 1) + 'd'
    });

    }, 'html');
    return false;
    });
    }

function submitFormWithAjax(form) {
  form = $(form);
  $.ajax({
    url: form.attr('action'),
    data: form.serialize(),
    type: (form.attr('method')),
    dataType: 'script',
    success: function(data){
   }
  });
  return false;
}

0 个答案:

没有答案