How to restrict Sencha touch Textfield to accept only numbers and space character?

638 Views Asked by At

I am trying to get only numbers and space in Text field of sencha touch. I am using this filed to enter credit card or debit card number. on keyup event I did some code to separate these numbers into 4 digit chunks. like 1234 5678 9012 3456 but I don't want to user enter alphabetical character like a,b,c,X,Y,Z and other. I Searched some solutions and used in my code but its doesn't seems to worinking. I used maskRe: /[0-9.]/, in textfiled config. but it is not working for me. any solution on this?

see below I placed some code of Textfiled

    this.CardNumber = new Ext.field.Text({
        label: TraxiApp.Lang.translate('_cardNo_'),
        labelAlign: 'top',
        clearIcon: false,
        maskRe: /[0-9.]/,
        cls: 'card-number-field',
        listeners: {
            scope: this,

            keyup: function (t) {

                var value = t.getValue().toString();
                var v = value.replace(/\s+/g, '').replace(/[^0-9]/gi, '')
                var matches = v.match(/\d{4,16}/g);
                var match = matches && matches[0] || ''
                var parts = []

                for (i = 0, len = match.length; i < len; i += 4) {
                    parts.push(match.substring(i, i + 4))
                }

                if (parts.length) {
                    var value = parts.join(' ')
                    t.setValue(value)
                } else {
                    t.setValue(value);
                }
            }
        }
    });
0

There are 0 best solutions below