Rency
Rency

Reputation: 404

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.

Upvotes: 1

Views: 3335

Answers (5)

Sergey S.
Sergey S.

Reputation: 1

{
    minValue: 0,
    autoStripChars: true,
    allowExponential: false
}

Upvotes: 0

tfentonz
tfentonz

Reputation: 564

Similar to @lagnat's reply which is a whitelist you can blacklist characters using stripCharsRe config.

stripCharsRe: /-/

If you are wanting to restrict negative values.

minValue: 0

Upvotes: 0

lagnat
lagnat

Reputation: 484

You could also use a regular Textfield with a maskRe config.

maskRe: /[0-9]/

Upvotes: 0

Jom
Jom

Reputation: 1897

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');
        }
    }
}

Upvotes: 1

sha
sha

Reputation: 17860

You need to create custom VType. VType validation is called after any change in the textfield and you have ability to configure what characters are allowed to be entered:

http://docs.sencha.com/ext-js/4-0/#!/api/Ext.form.field.VTypes

Take a look at xxxMask property.

Upvotes: 0

Related Questions