加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 06:33:51 所属栏目:百科 来源:网络整理
导读:我使用ExtJS Form中的numberField,只想输入正数,范围为0-99,它应该只接受2个字符(而不是2个)。 { xtype:"textfield",allowNegative: false,allowDecimals: false,minValue: 0,maxValue: 99,maxLength: 2} 在上面的代码中给出错误,但是它接受的2个字符。
我使用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属性,尽管我不认为这会阻止粘贴的无效值。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读