我使用ExtJS Form中的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>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
属性。