javascript EXTJS 日期字段验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11717605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 14:06:34  来源:igfitidea点击:

EXTJS date field validation

javascriptextjsextjs3

提问by user595234

In the EXTJS, I have a DateField. User entered 'abcd' to that field, if I getValue, it will return an empty string, not the 'abcd'.

在 EXTJS 中,我有一个 DateField。用户在该字段中输入了 'abcd',如果我 getValue,它将返回一个空字符串,而不是 'abcd'。

how can I force user to enter correct value for a dateField ? or how can I get the real input in that filed ?

如何强制用户为 dateField 输入正确的值?或者我怎样才能获得该文件中的真实输入?

Thanks.

谢谢。

回答by Johnny Rice

The datefield has also has a getRawValue method which is different from getValue because it does not apply any of the validation that getValue does.

datefield 也有一个 getRawValue 方法,它与 getValue 不同,因为它不应用 getValue 所做的任何验证。

To force a user to enter a correct value, you can use the reMask property. The default validation logic will allow invalid entry and will show that the entry is invalid with the red highlighting and such. The reMask masks filters keyboard input. In this example it restricts to numbers and forward slash and could be extended to any date format needed.

要强制用户输入正确的值,您可以使用 reMask 属性。默认验证逻辑将允许无效条目,并将显示该条目无效并以红色突出显示等。reMask 掩码过滤键盘输入。在这个例子中,它限制为数字和正斜杠,并且可以扩展到任何需要的日期格式。

Ext.create('Ext.form.Panel', {
    renderTo: Ext.getBody(),
    width: 300,
    bodyPadding: 10,
    title: 'Dates',
    items: [{
        xtype: 'datefield',
        anchor: '100%',
        fieldLabel: 'From',
        emptyText: 'mm/dd/yyyy',
        maskRe: /[0-9\/]/,
        name: 'from_date',
        maxValue: new Date()  // limited to the current date or prior
    }, {
        xtype: 'datefield',
        anchor: '100%',
        emptyText: 'mm/dd/yyyy',
        maskRe: /[0-9\/]/,
        fieldLabel: 'To',
        name: 'to_date',
        value: new Date()  // defaults to today
    }]
});?

回答by Danilo M.

When you use getValuemethod in a dateField the return will be: Wed Jul 25 2012 00:00:00 GMT-0300 (BRT), for example, but you can format the date using, Ext.util.Format.date(date, 'd/m/Y H:i:s');and the return will be "25/07/2012 00:00:00".

例如,当您getValue在 dateField 中使用方法时,返回将是:Wed Jul 25 2012 00:00:00 GMT-0300 (BRT),但您可以使用格式化日期,Ext.util.Format.date(date, 'd/m/Y H:i:s');返回将是"25/07/2012 00:00:00"