javascript ExtJs NumberField

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4957039/
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-25 15:28:51  来源:igfitidea点击:

ExtJs NumberField

javascriptextjsinputnumbers

提问by Some Java Guy

I am using ExtJs NumberField. The good thing is it validates that only numbers are entered. But I want a hyphen as well. How could I go about editing the javascript

我正在使用 ExtJs NumberField。好处是它验证只输入数字。但我也想要一个连字符。我怎么能去编辑 javascript

  var <portlet:namespace/>issueNoField = new Ext.form.NumberField({  //This takes only numbers
             fieldLabel: 'Issue No',
             width: 120,
             displayField: 'Enter no',  
             valueField:'IssNo'
         });

回答by Mchl

var <portlet:namespace/>issueNoField = new Ext.form.TextField({  //This takes only numbers
  fieldLabel: 'Issue No',
  width: 120,
  regex: /[1-9-]*/
});

回答by KomarSerjio

Use "baseChars" option.

使用“baseChars”选项。

     var <portlet:namespace/>issueNoField = new Ext.form.NumberField({  //This takes only numbers
         fieldLabel: 'Issue No',
         width: 120,
         displayField: 'Enter no',  
         valueField:'IssNo'
         baseChars: "0123456789-"
     });

回答by Nishant Bajracharya

Try the Fiddle

尝试小提琴

Its better to use the textfield and adding the maskRe attribute as

最好使用文本字段并将 maskRe 属性添加为

maskRe: /^[0-9 -]$/

maskRe: /^[0-9 -]$/

回答by kevin cline

Is the hyphen merely conventional, as in a US zip code (99999-9999)? In that case, you should accept only numeric input, insert the hyphen when that position is reached, and ignore any non-numeric characters.

连字符是否只是常规的,如美国邮政编码 (99999-9999)?在这种情况下,您应该只接受数字输入,到达该位置时插入连字符,并忽略任何非数字字符。

If they hyphen is significant, so that "12-345" and "123-45" are different inputs, then you are reading text and not a number. However, that would be an extremely unfriendly system.

如果它们的连字符很重要,因此“12-345”和“123-45”是不同的输入,那么您正在阅读文本而不是数字。然而,这将是一个非常不友好的系统。

回答by frazras

The Regex attribute never worked for me, I used the code below instead:

Regex 属性从来没有对我有用,我改用下面的代码:

maskRe: /[1-9-]/,

回答by BhandariS

{
xtype: 'textfield',
mask: '(999) 999-9999'
}

What I'm proposing here is that we use a text fieldand put a mask to it to capture only numeric values in the desires format.

我在这里提议的是,我们使用 atext field并为其放置一个掩码,以仅捕获所需格式的数字值。

You can mask it according to your needs.

您可以根据需要对其进行屏蔽。

For Example:

例如:

'9999999999'
'999-999-9999'

More infoThis will give the phone number in the format you want

更多信息这将以您想要的格式提供电话号码

回答by Christiaan Westerbeek

Just set the allowExponentialconfig of your numberfield to false. By default it is true and it adds 'e+-'and the decimalSeparatoras valid characters.

只需将allowExponentialnumberfield的配置设置为 false。默认情况下它是 true 并且它添加'e+-'decimalSeparator作为有效字符。

It's true. The source check the source.

这是真的。来源检查来源。