如何增加和减少textfield maxLength

时间:2018-05-17 12:36:47

标签: javascript extjs

我有textfield,我在其中声明maxLength: 15。在focusleave事件中,我添加了更多字符,例如,,所以我想增加textfield的长度。

我的代码是:

{
    xtype: 'textfield',
    name: 'email',
    fieldLabel: 'Email Address',
    maxLength: 15
    vtype: 'email' // requires value to be a valid email address format,
    listeners: {
        focusleave: function( this, event, eOpts)  {
            var myVAl = this.Value();
            myVAl.toLocaleString({
                minimumFractionDigits: 3
            });
        }
    }
}

在这里,如果我放了15个字符然后我想在每三个字符之后得到小数,所以我只得到13个使用完全字符。

那么如何在这种情况下增加和减少maxLength

1 个答案:

答案 0 :(得分:0)

  

那么如何增加和减少maxLength

是的,您可以使用focusenter更改foucuseleavetextfield.inputEl活动。

textfield.inputEl.dom.maxLength=15//Your dynamic value here

在此 FIDDLE 中,我使用相同的textfield.inputEl创建了一个演示。希望这有助于/指导您实现您的要求。

CODE SNIPPET

Ext.application({
    name: 'Fiddle',

    launch: function () {

        function stringInsert(str, n, joinwith) {
            var ret = [],
                i = 0,
                len = str.length;;

            for (; i < len; i += n) {
                ret.push(str.substr(i, n))
            }

            return ret.join(joinwith)
        };

        Ext.create({
            xtype: 'form',
            title: 'Demo Form',
            bodyPadding: 15,
            renderTo: Ext.getBody(),
            items: [{
                xtype: 'textfield',
                name: 'email',
                fieldLabel: 'Email Address',
                vtype: 'email', // requires value to be a valid email address format,
                listeners: {
                    focusenter: function (field) {
                        var value = field.getValue().replace(/\./g, '');
                        field.setValue(value);
                        field.inputEl.dom.maxLength=15;
                    },
                    focusleave: function (field, event, eOpts) {
                        var value = this.getValue(),
                            newValue = stringInsert(value, 3, '.');

                        field.inputEl.dom.maxLength=newValue.length;
                        field.setValue(newValue);
                    }
                }
            }]
        })
    }
});
相关问题