本文介绍了使用正则表达式限制textfield / numberfield中的输入字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ExtJS表单中使用numberField,只想输入正数,范围为0-99,它只能接受2个字符(不超过2个字符)。

  {
xtype:textfield,
allowNegative:false,
allowDecimals:false,
minValue:0,
maxValue:99,
maxLength:2
}

给出错误上面的代码,但它接受了两个以上的字符。



我也尝试过下面但问题是一样的:

  {
xtype:textfield,
regex:/ ^ \d {0,2} $ /,
regexText:< b>错误< / b>< / br>输入的无效数字。,
验证器:function(v){
return /^\d{0,2}$/.test(v)? true:无效数字;
}
}

如何限制输入多于2个字符? / p>

解决方案

如果您使用版本3,的 maxLength 文档描述使用 autoCreate 属性指定最大长度(文档示例显示NumberField,但也由TextField类支持):



  var myField = 
new Ext.form.NumberField({
id:'mobile',
anchor: '90%',
fieldLabel:'Mobile',
maxLength:16,//验证
autoCreate:{tag:'input',type:'text',size:'20 ',autocomplete:
'off',maxlength:'10'}});

使用版本4,TextField具有属性。 >

如果您使用正则表达式,两个版本都支持 maskRe 属性,尽管我不认为这会阻止粘贴的无效值。


I use numberField in ExtJS Form and want to enter only positive numbers, in range of 0-99 and it should accept only 2 characters (and not more than 2).

{
    xtype:"textfield",
    allowNegative: false,
    allowDecimals: false,
    minValue: 0,
    maxValue: 99,
    maxLength: 2
}

gives an error in above code but it is accepting more then 2 characters.

I also tried below but issue is same:

{
    xtype:"textfield",
    regex: /^\d{0,2}$/,
    regexText: "<b>Error</b></br>Invalid Number entered.",
    validator: function(v) {
        return /^\d{0,2}$/.test(v)?true:"Invalid Number";
    }
}

How to restrict to input more then 2 characters?

解决方案

if you're using version 3, the TextField's maxLength documentation describes using the autoCreate property to state the maximum length (the doc example shows a NumberField but it is also supported by the TextField class):

var myField =
new Ext.form.NumberField({
     id: 'mobile',
     anchor:'90%',
     fieldLabel: 'Mobile',
     maxLength: 16, // for validation
     autoCreate: {tag: 'input', type: 'text', size: '20', autocomplete:
'off', maxlength: '10'} });

Using version 4, a TextField has the enforceMaxLength property.

If you're set on using a regex, both versions support the maskRe property, although I don't think this prevents invalid values that are pasted.

这篇关于使用正则表达式限制textfield / numberfield中的输入字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 11:36