Search code examples
extjs4extjs4.1

How to restrict typing hyphen into extjs numberfield?


Currently extjs numberfield is allowing '-' hyphen otherthan numbers into the field. how to restrict that from typing? if i give custom vtype validation it is checking only after i submit that.


Solution

  • use autoStripChars and remove hyphen from allowed in initComponent. corrected code below.

    autoStripChars: true,
    initComponent: function() {
        var me = this,
            allowed;
    
        me.callParent();
    
        me.setMinValue(me.minValue);
        me.setMaxValue(me.maxValue);
    
        // Build regexes for masking and stripping based on the configured options
        if (me.disableKeyFilter !== true) {
            allowed = me.baseChars + '';
            if (me.allowDecimals) {
                allowed += me.decimalSeparator;
            }
    /* removed code
        if (me.minValue < 0) {
                allowed += '-';
            } 
    */
            allowed = Ext.String.escapeRegex(allowed);
            me.maskRe = new RegExp('[' + allowed + ']');
            if (me.autoStripChars) {
                me.stripCharsRe = new RegExp('[^' + allowed + ']', 'gi');
            }
        }
    }