DatePicker上的日期验证器触发IE7/IE8中的否定否定 [英] Date validator on DatePicker trigger false negatives in IE7/IE8

查看:87
本文介绍了DatePicker上的日期验证器触发IE7/IE8中的否定否定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对包含两个jQuery UI DatePickers的表单进行了一些基本验证.日期的格式为yy-mm-dd.两个DatePickers上都有一个 required date 验证.

I have some basic validation on a form which includes two jQuery UI DatePickers. The format of the date is yy-mm-dd. There is a required and date validation on both DatePickers.

这些工作在Chrome& FF,但在IE7/IE8中触发假阴性(称有效输入为无效).

These work as expected in Chrome & FF, but trigger false negatives (valid input is said to be invalid) in IE7/IE8.

日期选择器设置:

$('.datepicker').datepicker({
    dateFormat: 'yy-mm-dd'
});

这无关紧要,但我想我会包括在内,以防万一:

This is unrelated but I figured I would include, just in case:

$.validator.addMethod("endDate", function(value, element) {
    var startDate = $('#startDate').val();
    return Date.parse(startDate) <= Date.parse(value);
});

实际验证:

$('#ExampleForm').validate({
    rules: {    
        StartDate: {
            required: true,
            date: true
        },
        EndDate: {
            required: true,
            date: true,
            endDate: true
        }
    },
    messages: {
        StartDate: {
            required: "Start Date required",
            date: "Invalid date. Must be formatted yyyy-mm-dd"
        },
        EndDate: {
            required: "End Date required",
            date: "Invalid date. Must be formatted yyyy-mm-dd",
            endDate: "Start date must occur before end date."
        }
    },
    errorPlacement: function(error, element) {
        error.appendTo(element.parent().next());
    },
    submitHandle: function(form) {
        form.submit();
    }
});

在IE7/IE8中,使用两个DatePickers进行有效输入(仅选择一个日期)将导致 date 错误(无效日期.必须将格式设置为yyyy-mm-dd").在其他浏览器中不会发生这种情况.

In IE7/IE8, valid input (just picking a date) with both DatePickers will result in the date error ("Invalid date. Must be formatted yyyy-mm-dd"). This does not occur in other browsers.

它也不会产生任何Javascript错误.

It also doesn't produce any Javascript errors.

预先感谢

伊恩

推荐答案

我认为您正在寻找 dateISO 选项:

I think you're looking for the dateISO option:

$('form').validate({
    rules: {
        StartDate: {
            required: true,
            dateISO: true
        },
        EndDate: {
            required: true,
            dateISO: true
        }
    },
    messages: {
        StartDate: {
            required: "Start Date required",
            dateISO: "Invalid date. Must be formatted yyyy-mm-dd"
        },
        EndDate: {
            required: "End Date required",
            dateISO: "Invalid date. Must be formatted yyyy-mm-dd"
        }
    },
    submitHandler: function(form) {
        form.submit();
    }
});

IE不会以yyyy-mm-dd格式解析日期,这就是为什么在IE中无法使用常规date的原因.我相信jQuery validate仅使用Date.parsenew Date(dateString)来检查有效性.要检查这一点,请尝试执行new Date("1987-11-14")并警告IE和FF中的值.您将在IE中得到NaN,在FF中得到一个日期对象.

IE won't parse dates in yyyy-mm-dd format, which is why using regular date fails in IE. I believe jQuery validate just uses Date.parse or new Date(dateString) to check for validity. To check this, try doing new Date("1987-11-14") and alerting the value in IE and FF. You'll get NaN in IE and a date object in FF.

这是一个工作示例: http://jsfiddle.net/andrewwhitaker/QqSrJ/2/

这篇关于DatePicker上的日期验证器触发IE7/IE8中的否定否定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆