2 * Ext JS Library 2.2.1
\r
3 * Copyright(c) 2006-2009, Ext JS, LLC.
\r
4 * licensing@extjs.com
\r
6 * http://extjs.com/license
\r
10 * @class Ext.form.NumberField
\r
11 * @extends Ext.form.TextField
\r
12 * Numeric text field that provides automatic keystroke filtering and numeric validation.
\r
14 * Creates a new NumberField
\r
15 * @param {Object} config Configuration options
\r
17 Ext.form.NumberField = Ext.extend(Ext.form.TextField, {
\r
19 * @cfg {RegExp} stripCharsRe @hide
\r
22 * @cfg {String} fieldClass The default CSS class for the field (defaults to "x-form-field x-form-num-field")
\r
24 fieldClass: "x-form-field x-form-num-field",
\r
26 * @cfg {Boolean} allowDecimals False to disallow decimal values (defaults to true)
\r
28 allowDecimals : true,
\r
30 * @cfg {String} decimalSeparator Character(s) to allow as the decimal separator (defaults to '.')
\r
32 decimalSeparator : ".",
\r
34 * @cfg {Number} decimalPrecision The maximum precision to display after the decimal separator (defaults to 2)
\r
36 decimalPrecision : 2,
\r
38 * @cfg {Boolean} allowNegative False to prevent entering a negative sign (defaults to true)
\r
40 allowNegative : true,
\r
42 * @cfg {Number} minValue The minimum allowed value (defaults to Number.NEGATIVE_INFINITY)
\r
44 minValue : Number.NEGATIVE_INFINITY,
\r
46 * @cfg {Number} maxValue The maximum allowed value (defaults to Number.MAX_VALUE)
\r
48 maxValue : Number.MAX_VALUE,
\r
50 * @cfg {String} minText Error text to display if the minimum value validation fails (defaults to "The minimum value for this field is {minValue}")
\r
52 minText : "The minimum value for this field is {0}",
\r
54 * @cfg {String} maxText Error text to display if the maximum value validation fails (defaults to "The maximum value for this field is {maxValue}")
\r
56 maxText : "The maximum value for this field is {0}",
\r
58 * @cfg {String} nanText Error text to display if the value is not a valid number. For example, this can happen
\r
59 * if a valid character like '.' or '-' is left in the field with no number (defaults to "{value} is not a valid number")
\r
61 nanText : "{0} is not a valid number",
\r
63 * @cfg {String} baseChars The base set of characters to evaluate as valid numbers (defaults to '0123456789').
\r
65 baseChars : "0123456789",
\r
68 initEvents : function(){
\r
69 Ext.form.NumberField.superclass.initEvents.call(this);
\r
70 var allowed = this.baseChars+'';
\r
71 if(this.allowDecimals){
\r
72 allowed += this.decimalSeparator;
\r
74 if(this.allowNegative){
\r
77 this.stripCharsRe = new RegExp('[^'+allowed+']', 'gi');
\r
78 var keyPress = function(e){
\r
80 if(!Ext.isIE && (e.isSpecialKey() || k == e.BACKSPACE || k == e.DELETE)){
\r
83 var c = e.getCharCode();
\r
84 if(allowed.indexOf(String.fromCharCode(c)) === -1){
\r
88 this.el.on("keypress", keyPress, this);
\r
92 validateValue : function(value){
\r
93 if(!Ext.form.NumberField.superclass.validateValue.call(this, value)){
\r
96 if(value.length < 1){ // if it's blank and textfield didn't flag it then it's valid
\r
99 value = String(value).replace(this.decimalSeparator, ".");
\r
101 this.markInvalid(String.format(this.nanText, value));
\r
104 var num = this.parseValue(value);
\r
105 if(num < this.minValue){
\r
106 this.markInvalid(String.format(this.minText, this.minValue));
\r
109 if(num > this.maxValue){
\r
110 this.markInvalid(String.format(this.maxText, this.maxValue));
\r
116 getValue : function(){
\r
117 return this.fixPrecision(this.parseValue(Ext.form.NumberField.superclass.getValue.call(this)));
\r
120 setValue : function(v){
\r
121 v = typeof v == 'number' ? v : parseFloat(String(v).replace(this.decimalSeparator, "."));
\r
122 v = isNaN(v) ? '' : String(v).replace(".", this.decimalSeparator);
\r
123 Ext.form.NumberField.superclass.setValue.call(this, v);
\r
127 parseValue : function(value){
\r
128 value = parseFloat(String(value).replace(this.decimalSeparator, "."));
\r
129 return isNaN(value) ? '' : value;
\r
133 fixPrecision : function(value){
\r
134 var nan = isNaN(value);
\r
135 if(!this.allowDecimals || this.decimalPrecision == -1 || nan || !value){
\r
136 return nan ? '' : value;
\r
138 return parseFloat(parseFloat(value).toFixed(this.decimalPrecision));
\r
141 beforeBlur : function(){
\r
142 var v = this.parseValue(this.getRawValue());
\r
144 this.setValue(this.fixPrecision(v));
\r
148 Ext.reg('numberfield', Ext.form.NumberField);