2

It seems very easy to force the decimal to some precision and their are many ways.I have tried using all as:

1st way : Using config

            xtype: 'numberfield',
            fieldLabel: 'Balance',
            name: 'balancefield',    
            decimalPrecision:2

Alternative : by overrriding or extending as shown here

Or using regular expressions

All of these work on blur or some other event or function.Is their a way to stop the input after two decimals. In textfields we can use RegEx and maskRe configs.But in numberfield,is their anything like maskRe,so that user cannot enter the more than two decimals. Any help is appreciated.Thanks.

Community
  • 1
  • 1
Dev
  • 3,922
  • 3
  • 24
  • 44

1 Answers1

2

The maskRe is indeed used internally by the number field, so you'll have to mimick its behaviour. That is, listening the keypress event of the field, and stop the event if the input does fits you.

Example, for enforcing two digits decimal precision using a regex:

Ext.widget('numberfield', {
    renderTo: Ext.getBody()
    ,enableKeyEvents: true
    ,listeners: {
        keypress: function(field, e) {
            var v = field.getValue();
            if (!Ext.isEmpty(v)) {
                if (/\.\d{2,}/.test(v)) {
                    e.stopEvent();
                }
            }
        }
    }
});
rixo
  • 23,815
  • 4
  • 63
  • 68
  • Thnks rixo. I actually tried this before,but missed **e.stopEvent();** – Dev Jun 04 '13 at 07:42
  • just having an issue.Is their anything to restrict the digits before decimal to 15.I have tried with different regex in above solution you have provided with.Can you please help me with this.Thnks again – Dev Jun 04 '13 at 11:43
  • The above solution is having some issue > I am able to enter three 0's followed by any no. of digits after decimal. – Dev Jun 07 '13 at 06:03