Upgrade to ExtJS 3.3.1 - Released 11/30/2010
[extjs.git] / src / widgets / form / NumberField.js
1 /*!
2  * Ext JS Library 3.3.1
3  * Copyright(c) 2006-2010 Sencha Inc.
4  * licensing@sencha.com
5  * http://www.sencha.com/license
6  */
7 /**
8  * @class Ext.form.NumberField
9  * @extends Ext.form.TextField
10  * Numeric text field that provides automatic keystroke filtering and numeric validation.
11  * @constructor
12  * Creates a new NumberField
13  * @param {Object} config Configuration options
14  * @xtype numberfield
15  */
16 Ext.form.NumberField = Ext.extend(Ext.form.TextField,  {
17     /**
18      * @cfg {RegExp} stripCharsRe @hide
19      */
20     /**
21      * @cfg {RegExp} maskRe @hide
22      */
23     /**
24      * @cfg {String} fieldClass The default CSS class for the field (defaults to "x-form-field x-form-num-field")
25      */
26     fieldClass: "x-form-field x-form-num-field",
27     
28     /**
29      * @cfg {Boolean} allowDecimals False to disallow decimal values (defaults to true)
30      */
31     allowDecimals : true,
32     
33     /**
34      * @cfg {String} decimalSeparator Character(s) to allow as the decimal separator (defaults to '.')
35      */
36     decimalSeparator : ".",
37     
38     /**
39      * @cfg {Number} decimalPrecision The maximum precision to display after the decimal separator (defaults to 2)
40      */
41     decimalPrecision : 2,
42     
43     /**
44      * @cfg {Boolean} allowNegative False to prevent entering a negative sign (defaults to true)
45      */
46     allowNegative : true,
47     
48     /**
49      * @cfg {Number} minValue The minimum allowed value (defaults to Number.NEGATIVE_INFINITY)
50      */
51     minValue : Number.NEGATIVE_INFINITY,
52     
53     /**
54      * @cfg {Number} maxValue The maximum allowed value (defaults to Number.MAX_VALUE)
55      */
56     maxValue : Number.MAX_VALUE,
57     
58     /**
59      * @cfg {String} minText Error text to display if the minimum value validation fails (defaults to "The minimum value for this field is {minValue}")
60      */
61     minText : "The minimum value for this field is {0}",
62     
63     /**
64      * @cfg {String} maxText Error text to display if the maximum value validation fails (defaults to "The maximum value for this field is {maxValue}")
65      */
66     maxText : "The maximum value for this field is {0}",
67     
68     /**
69      * @cfg {String} nanText Error text to display if the value is not a valid number.  For example, this can happen
70      * if a valid character like '.' or '-' is left in the field with no number (defaults to "{value} is not a valid number")
71      */
72     nanText : "{0} is not a valid number",
73     
74     /**
75      * @cfg {String} baseChars The base set of characters to evaluate as valid numbers (defaults to '0123456789').
76      */
77     baseChars : "0123456789",
78     
79     /**
80      * @cfg {Boolean} autoStripChars True to automatically strip not allowed characters from the field. Defaults to <tt>false</tt>
81      */
82     autoStripChars: false,
83
84     // private
85     initEvents : function() {
86         var allowed = this.baseChars + '';
87         if (this.allowDecimals) {
88             allowed += this.decimalSeparator;
89         }
90         if (this.allowNegative) {
91             allowed += '-';
92         }
93         allowed = Ext.escapeRe(allowed);
94         this.maskRe = new RegExp('[' + allowed + ']');
95         if (this.autoStripChars) {
96             this.stripCharsRe = new RegExp('[^' + allowed + ']', 'gi');
97         }
98         
99         Ext.form.NumberField.superclass.initEvents.call(this);
100     },
101     
102     /**
103      * Runs all of NumberFields validations and returns an array of any errors. Note that this first
104      * runs TextField's validations, so the returned array is an amalgamation of all field errors.
105      * The additional validations run test that the value is a number, and that it is within the
106      * configured min and max values.
107      * @param {Mixed} value The value to get errors for (defaults to the current field value)
108      * @return {Array} All validation errors for this field
109      */
110     getErrors: function(value) {
111         var errors = Ext.form.NumberField.superclass.getErrors.apply(this, arguments);
112         
113         value = Ext.isDefined(value) ? value : this.processValue(this.getRawValue());
114         
115         if (value.length < 1) { // if it's blank and textfield didn't flag it then it's valid
116              return errors;
117         }
118         
119         value = String(value).replace(this.decimalSeparator, ".");
120         
121         if(isNaN(value)){
122             errors.push(String.format(this.nanText, value));
123         }
124         
125         var num = this.parseValue(value);
126         
127         if (num < this.minValue) {
128             errors.push(String.format(this.minText, this.minValue));
129         }
130         
131         if (num > this.maxValue) {
132             errors.push(String.format(this.maxText, this.maxValue));
133         }
134         
135         return errors;
136     },
137
138     getValue : function() {
139         return this.fixPrecision(this.parseValue(Ext.form.NumberField.superclass.getValue.call(this)));
140     },
141
142     setValue : function(v) {
143         v = this.fixPrecision(v);
144         v = Ext.isNumber(v) ? v : parseFloat(String(v).replace(this.decimalSeparator, "."));
145         v = isNaN(v) ? '' : String(v).replace(".", this.decimalSeparator);
146         return Ext.form.NumberField.superclass.setValue.call(this, v);
147     },
148     
149     /**
150      * Replaces any existing {@link #minValue} with the new value.
151      * @param {Number} value The minimum value
152      */
153     setMinValue : function(value) {
154         this.minValue = Ext.num(value, Number.NEGATIVE_INFINITY);
155     },
156     
157     /**
158      * Replaces any existing {@link #maxValue} with the new value.
159      * @param {Number} value The maximum value
160      */
161     setMaxValue : function(value) {
162         this.maxValue = Ext.num(value, Number.MAX_VALUE);    
163     },
164
165     // private
166     parseValue : function(value) {
167         value = parseFloat(String(value).replace(this.decimalSeparator, "."));
168         return isNaN(value) ? '' : value;
169     },
170
171     /**
172      * @private
173      * 
174      */
175     fixPrecision : function(value) {
176         var nan = isNaN(value);
177         
178         if (!this.allowDecimals || this.decimalPrecision == -1 || nan || !value) {
179             return nan ? '' : value;
180         }
181         
182         return parseFloat(parseFloat(value).toFixed(this.decimalPrecision));
183     },
184
185     beforeBlur : function() {
186         var v = this.parseValue(this.getRawValue());
187         
188         if (!Ext.isEmpty(v)) {
189             this.setValue(v);
190         }
191     }
192 });
193
194 Ext.reg('numberfield', Ext.form.NumberField);