user1234
user1234

Reputation: 3159

How to prevent from entering decimals and negatives in the Ext textfield-- Extjs

I have a textfield that is accepting large input numbers. My issue is if I use Ext.form.NumerField({}) the large numbers gets converted into 1e notation and hence I'm unable to fix this with toFixed(). Hence I switched to using Ext.form.TextField({}). However with textfield I want to prevent user from entering decimal numbers, negative numbers and allow only numeric fields. For only numeric I can use regex: var regex = /[0-9.]/,. // will only allow numeric values But I'm not sure how can I get all the above conditions(no decimals and no negative) in the regex.

any help on regex will be appreciated.

Thanks

Upvotes: 0

Views: 518

Answers (1)

Fabio Barros
Fabio Barros

Reputation: 1439

If you want to just validate the input, use the following regex:

/^\d+$/

but, if you prevent typing, add this on the text field:

    enableKeyEvents:true,
    listeners:{
            keydown : function(t,e) {
            var code = e.browserEvent.keyCode;
            console.log(code);     
            if ((code < 48 || code > 57) &&(code !== 8)){
                e.stopEvent();
            }
        }
    }, 

Upvotes: 2

Related Questions