微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

正则表达式 – 使用正则表达式限制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属性,尽管我不认为这会阻止粘贴的无效值。

原文地址:https://www.jb51.cc/regex/357405.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐