initial import
[roojs1] / Roo / form / NumberField.js
1 /*
2  * Based on:
3  * Ext JS Library 1.1.1
4  * Copyright(c) 2006-2007, Ext JS, LLC.
5  *
6  * Originally Released Under LGPL - original licence link has changed is not relivant.
7  *
8  * Fork - LGPL
9  * <script type="text/javascript">
10  */
11  
12
13 /**
14  * @class Roo.form.NumberField
15  * @extends Roo.form.TextField
16  * Numeric text field that provides automatic keystroke filtering and numeric validation.
17  * @constructor
18  * Creates a new NumberField
19  * @param {Object} config Configuration options
20  */
21 Roo.form.NumberField = function(config){
22     Roo.form.NumberField.superclass.constructor.call(this, config);
23 };
24
25 Roo.extend(Roo.form.NumberField, Roo.form.TextField,  {
26     /**
27      * @cfg {String} fieldClass The default CSS class for the field (defaults to "x-form-field x-form-num-field")
28      */
29     fieldClass: "x-form-field x-form-num-field",
30     /**
31      * @cfg {Boolean} allowDecimals False to disallow decimal values (defaults to true)
32      */
33     allowDecimals : true,
34     /**
35      * @cfg {String} decimalSeparator Character(s) to allow as the decimal separator (defaults to '.')
36      */
37     decimalSeparator : ".",
38     /**
39      * @cfg {Number} decimalPrecision The maximum precision to display after the decimal separator (defaults to 2)
40      */
41     decimalPrecision : 2,
42     /**
43      * @cfg {Boolean} allowNegative False to prevent entering a negative sign (defaults to true)
44      */
45     allowNegative : true,
46     /**
47      * @cfg {Number} minValue The minimum allowed value (defaults to Number.NEGATIVE_INFINITY)
48      */
49     minValue : Number.NEGATIVE_INFINITY,
50     /**
51      * @cfg {Number} maxValue The maximum allowed value (defaults to Number.MAX_VALUE)
52      */
53     maxValue : Number.MAX_VALUE,
54     /**
55      * @cfg {String} minText Error text to display if the minimum value validation fails (defaults to "The minimum value for this field is {minValue}")
56      */
57     minText : "The minimum value for this field is {0}",
58     /**
59      * @cfg {String} maxText Error text to display if the maximum value validation fails (defaults to "The maximum value for this field is {maxValue}")
60      */
61     maxText : "The maximum value for this field is {0}",
62     /**
63      * @cfg {String} nanText Error text to display if the value is not a valid number.  For example, this can happen
64      * if a valid character like '.' or '-' is left in the field with no number (defaults to "{value} is not a valid number")
65      */
66     nanText : "{0} is not a valid number",
67
68     // private
69     initEvents : function(){
70         Roo.form.NumberField.superclass.initEvents.call(this);
71         var allowed = "0123456789";
72         if(this.allowDecimals){
73             allowed += this.decimalSeparator;
74         }
75         if(this.allowNegative){
76             allowed += "-";
77         }
78         this.stripCharsRe = new RegExp('[^'+allowed+']', 'gi');
79         var keyPress = function(e){
80             var k = e.getKey();
81             if(!Roo.isIE && (e.isSpecialKey() || k == e.BACKSPACE || k == e.DELETE)){
82                 return;
83             }
84             var c = e.getCharCode();
85             if(allowed.indexOf(String.fromCharCode(c)) === -1){
86                 e.stopEvent();
87             }
88         };
89         this.el.on("keypress", keyPress, this);
90     },
91
92     // private
93     validateValue : function(value){
94         if(!Roo.form.NumberField.superclass.validateValue.call(this, value)){
95             return false;
96         }
97         if(value.length < 1){ // if it's blank and textfield didn't flag it then it's valid
98              return true;
99         }
100         var num = this.parseValue(value);
101         if(isNaN(num)){
102             this.markInvalid(String.format(this.nanText, value));
103             return false;
104         }
105         if(num < this.minValue){
106             this.markInvalid(String.format(this.minText, this.minValue));
107             return false;
108         }
109         if(num > this.maxValue){
110             this.markInvalid(String.format(this.maxText, this.maxValue));
111             return false;
112         }
113         return true;
114     },
115
116     getValue : function(){
117         return this.fixPrecision(this.parseValue(Roo.form.NumberField.superclass.getValue.call(this)));
118     },
119
120     // private
121     parseValue : function(value){
122         value = parseFloat(String(value).replace(this.decimalSeparator, "."));
123         return isNaN(value) ? '' : value;
124     },
125
126     // private
127     fixPrecision : function(value){
128         var nan = isNaN(value);
129         if(!this.allowDecimals || this.decimalPrecision == -1 || nan || !value){
130             return nan ? '' : value;
131         }
132         return parseFloat(value).toFixed(this.decimalPrecision);
133     },
134
135     setValue : function(v){
136         Roo.form.NumberField.superclass.setValue.call(this, String(v).replace(".", this.decimalSeparator));
137     },
138
139     // private
140     decimalPrecisionFcn : function(v){
141         return Math.floor(v);
142     },
143
144     beforeBlur : function(){
145         var v = this.parseValue(this.getRawValue());
146         if(v){
147             this.setValue(this.fixPrecision(v));
148         }
149     }
150 });