正则表达式 – 使用正则表达式限制textfield/numberfield中的输入字符?

我使用ExtJS Form中的numberField,只想输入正数,范围为0-99,它应该只接受2个字符(而不是2个)。
{
    xtype:"textfield",allowNegative: false,allowDecimals: false,minValue: 0,maxValue: 99,maxLength: 2
}

在上面的代码中给出错误,但是它接受的2个字符。

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

{
    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";
    }
}

如何限制输入超过2个字符?

如果您使用版本3,则 TextField的maxLength文档描述了使用autocreate属性来声明最大长度(文档示例显示的是NumberField,但它也受TextField类支持):

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'} });

使用版本4,TextField具有enforceMaxLength属性

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

相关文章

正则替换html代码中img标签的src值在开发富文本信息在移动端...
正则表达式
AWK是一种处理文本文件的语言,是一个强大的文件分析工具。它...
正则表达式是特殊的字符序列,利用事先定义好的特定字符以及...
Python界一名小学生,热心分享编程学习。
收集整理每周优质开发者内容,包括、、等方面。每周五定期发...