使用正则表达式限制textfield / numberfield中的输入字符? [英] Restrict the input characters in textfield / numberfield using regular expression?

查看:116
本文介绍了使用正则表达式限制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, TextField maxLength 文档描述使用 autoCreate 属性指定最大长度(文档示例显示NumberField,但也由TextField类支持):


maxLength:Number验证允许的最大输入字段长度
(默认为Number.MAX_VALUE)。此行为旨在通过提高可用性以允许粘贴
和编辑或重新设计和返回跟踪来向用户提供
即时反馈。要限制可以输入字段的最大
个字符数,可以使用autoCreate
添加要添加到字段的任何属性,例如:




  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具有 enforceMaxLength 属性。 >

如果您使用正则表达式,两个版本都支持 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):

maxLength : Number Maximum input field length allowed by validation (defaults to Number.MAX_VALUE). This behavior is intended to provide instant feedback to the user by improving usability to allow pasting and editing or overtyping and back tracking. To restrict the maximum number of characters that can be entered into the field use autoCreate to add any attributes you want to a field, for example:

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中的输入字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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