datefield将无效日期转换为有效日期

datefield将无效日期转换为有效日期

本文介绍了ExtJs datefield将无效日期转换为有效日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ExtJS 2.1,我有以下问题,我讨厌'datefield'。现在必须以MM / DD / YYYY格式输入日期。问题是如果用户输入21/17或16/05这样的东西,就会转换成有效的日期。 (21/17转换为9/17/2015,16/05转换为4/05/2015)。如何覆盖此行为?我试着写我自己的验证器,但是也没有帮助,即使我的验证器返回'false',转换仍然发生。以下是以下代码:

I am using ExtJS 2.1 and I have the following problem, I hate a 'datefield'. Now the date has to be entered in the format 'MM/DD/YYYY'. The problem is if the user enters something like '21/17' or '16/05' it gets converted to a valid date. (21/17 gets converted to 9/17/2015 and 16/05 gets converted to 4/05/2015). How do I override this behavior? I tried writing my own validator but that didn't help either, even if my validator returns 'false' the conversion still happens. Here is the code below:

var d = new Ext.form.DateField({
            el: el.dom,
            id: id,
            format: 'm/d/Y',
            hideTrigger: false,
            allowBlank: true,
            disabled: isDisabled,
            validator: testForShortDate,
            validateOnBlur: true,
            minLength:6,
            //validationEvent: false,  //string or boolean
            invalidText: 'Enter date as MM/DD/YYYY',
            menuListeners: Ext.applyIf({
                select: function (m, d) {
                    Ext.form.DateField.prototype.menuListeners.select.apply(this, arguments);
                    this.focus.defer(100, this);
                    onDateSelect(m, d, this);
                }
            })
        });

        d.render();

d

function testForShortDate(date) {
if (date.split("/").length != 3) {
    console.log(date.split("/").length);
    return false;
}
return true;

任何人都可以帮忙?

推荐答案

如果日期字段的值不能使用配置的。这些格式可以使用属性。

There are alternative date formats which ExtJS will try to use if the datefield's value cannot be parsed using the configured format. These formats can be defined using the altFormats property.

默认值为:

m/d/Y|n/j/Y|n/j/y|m/j/y|n/d/y|m/j/Y|n/d/Y|m-d-y|m-d-Y|m/d|m-d|md|mdy|mdY|d|Y-m-d

这解释了为什么像 21/17 之类的东西被转换为 9/17/2015 ,作为格式 m / d 在这里(2014年的第21个月真的是2015年第9期)。

which explains why something like 21/17 gets converted to 9/17/2015, as the format m/d is used here (the "21st" month of 2014 is really the 9th of 2015).

如果你想全部禁用,只需将该属性设置为一个空字符串:

If you want to disable this altogether, just set the property to an empty string:

altFormats: ''

这篇关于ExtJs datefield将无效日期转换为有效日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 20:30