3 * Copyright(c) 2006-2010 Ext JS, Inc.
5 * http://www.extjs.com/license
8 * @class Ext.form.NumberField
9 * @extends Ext.form.TextField
10 * Numeric text field that provides automatic keystroke filtering and numeric validation.
12 * Creates a new NumberField
13 * @param {Object} config Configuration options
16 Ext.form.NumberField = Ext.extend(Ext.form.TextField, {
18 * @cfg {RegExp} stripCharsRe @hide
21 * @cfg {RegExp} maskRe @hide
24 * @cfg {String} fieldClass The default CSS class for the field (defaults to "x-form-field x-form-num-field")
26 fieldClass: "x-form-field x-form-num-field",
28 * @cfg {Boolean} allowDecimals False to disallow decimal values (defaults to true)
32 * @cfg {String} decimalSeparator Character(s) to allow as the decimal separator (defaults to '.')
34 decimalSeparator : ".",
36 * @cfg {Number} decimalPrecision The maximum precision to display after the decimal separator (defaults to 2)
40 * @cfg {Boolean} allowNegative False to prevent entering a negative sign (defaults to true)
44 * @cfg {Number} minValue The minimum allowed value (defaults to Number.NEGATIVE_INFINITY)
46 minValue : Number.NEGATIVE_INFINITY,
48 * @cfg {Number} maxValue The maximum allowed value (defaults to Number.MAX_VALUE)
50 maxValue : Number.MAX_VALUE,
52 * @cfg {String} minText Error text to display if the minimum value validation fails (defaults to "The minimum value for this field is {minValue}")
54 minText : "The minimum value for this field is {0}",
56 * @cfg {String} maxText Error text to display if the maximum value validation fails (defaults to "The maximum value for this field is {maxValue}")
58 maxText : "The maximum value for this field is {0}",
60 * @cfg {String} nanText Error text to display if the value is not a valid number. For example, this can happen
61 * if a valid character like '.' or '-' is left in the field with no number (defaults to "{value} is not a valid number")
63 nanText : "{0} is not a valid number",
65 * @cfg {String} baseChars The base set of characters to evaluate as valid numbers (defaults to '0123456789').
67 baseChars : "0123456789",
70 initEvents : function(){
71 var allowed = this.baseChars + '';
72 if (this.allowDecimals) {
73 allowed += this.decimalSeparator;
75 if (this.allowNegative) {
78 this.maskRe = new RegExp('[' + Ext.escapeRe(allowed) + ']');
79 Ext.form.NumberField.superclass.initEvents.call(this);
83 * Runs all of NumberFields validations and returns an array of any errors. Note that this first
84 * runs TextField's validations, so the returned array is an amalgamation of all field errors.
85 * The additional validations run test that the value is a number, and that it is within the
86 * configured min and max values.
87 * @param {Mixed} value The value to get errors for (defaults to the current field value)
88 * @return {Array} All validation errors for this field
90 getErrors: function(value) {
91 var errors = Ext.form.NumberField.superclass.getErrors.apply(this, arguments);
93 value = value || this.processValue(this.getRawValue());
95 if (value.length < 1) { // if it's blank and textfield didn't flag it then it's valid
99 value = String(value).replace(this.decimalSeparator, ".");
102 errors.push(String.format(this.nanText, value));
105 var num = this.parseValue(value);
107 if(num < this.minValue){
108 errors.push(String.format(this.minText, this.minValue));
111 if(num > this.maxValue){
112 errors.push(String.format(this.maxText, this.maxValue));
118 getValue : function(){
119 return this.fixPrecision(this.parseValue(Ext.form.NumberField.superclass.getValue.call(this)));
122 setValue : function(v){
123 v = Ext.isNumber(v) ? v : parseFloat(String(v).replace(this.decimalSeparator, "."));
124 v = isNaN(v) ? '' : String(v).replace(".", this.decimalSeparator);
125 return Ext.form.NumberField.superclass.setValue.call(this, v);
129 * Replaces any existing {@link #minValue} with the new value.
130 * @param {Number} value The minimum value
132 setMinValue : function(value){
133 this.minValue = Ext.num(value, Number.NEGATIVE_INFINITY);
137 * Replaces any existing {@link #maxValue} with the new value.
138 * @param {Number} value The maximum value
140 setMaxValue : function(value){
141 this.maxValue = Ext.num(value, Number.MAX_VALUE);
145 parseValue : function(value){
146 value = parseFloat(String(value).replace(this.decimalSeparator, "."));
147 return isNaN(value) ? '' : value;
151 fixPrecision : function(value){
152 var nan = isNaN(value);
153 if(!this.allowDecimals || this.decimalPrecision == -1 || nan || !value){
154 return nan ? '' : value;
156 return parseFloat(parseFloat(value).toFixed(this.decimalPrecision));
159 beforeBlur : function(){
160 var v = this.parseValue(this.getRawValue());
162 this.setValue(this.fixPrecision(v));
166 Ext.reg('numberfield', Ext.form.NumberField);