Fix #8117 - another go at thousand seperator
[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 {String} thousandSeparator Character(s) to allow as the thousand separator (defaults to '') - set to ',' for example
40      */
41     thousandSeparator : "",
42     /**
43      * @cfg {Number} decimalPrecision The maximum precision to display after the decimal separator (defaults to 2)
44      */
45     decimalPrecision : 2,
46     /**
47      * @cfg {Boolean} allowNegative False to prevent entering a negative sign (defaults to true)
48      */
49     allowNegative : true,
50     /**
51      * @cfg {Number} minValue The minimum allowed value (defaults to Number.NEGATIVE_INFINITY)
52      */
53     minValue : Number.NEGATIVE_INFINITY,
54     /**
55      * @cfg {Number} maxValue The maximum allowed value (defaults to Number.MAX_VALUE)
56      */
57     maxValue : Number.MAX_VALUE,
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      * @cfg {String} maxText Error text to display if the maximum value validation fails (defaults to "The maximum value for this field is {maxValue}")
64      */
65     maxText : "The maximum value for this field is {0}",
66     /**
67      * @cfg {String} nanText Error text to display if the value is not a valid number.  For example, this can happen
68      * if a valid character like '.' or '-' is left in the field with no number (defaults to "{value} is not a valid number")
69      */
70     nanText : "{0} is not a valid number",
71     
72     hiddenField : false,
73      
74     onRender : function(ct, position)
75     {
76         Roo.form.TextField.superclass.onRender.call(this, ct, position);
77     
78             //this.el.dom.removeAttribute('name'); 
79         Roo.log("Changing name?");
80         if (this.thousandSeparator != '') {
81             this.el.dom.setAttribute('name', this.name + '____hidden___' ); 
82             this.hiddenField = this.el.insertSibling({ tag:'input', type:'hidden', name: this.name },
83                         'before', true);
84             this.hiddenField.value = this.value ? this.parseValue(this.value) : '';
85             this.el.on('blur', this.onBlur, this);
86         }
87         
88             // prevent input submission
89         
90             
91             
92     },
93      onBlur : function(){
94         this.beforeBlur();
95         if(!Roo.isOpera && this.focusClass){ // don't touch in Opera
96             this.el.removeClass(this.focusClass);
97         }
98         this.hasFocus = false;
99         if(this.validationEvent !== false && this.validateOnBlur && this.validationEvent != "blur"){
100             this.validate();
101         }
102         var v = this.getValue();
103         if(String(v) !== String(this.startValue)){
104             this.setValue( this.parseValue(v));
105             this.fireEvent('change', this, v, this.startValue);
106         }
107         this.fireEvent("blur", this);
108     },
109     
110     // override name, so that it works with hidden field.
111     getName: function(){
112         if (this.thousandSeparator != '') {
113             return this.name;
114         }
115         return Roo.form.TextField.superclass.getName.call(this);
116     },
117     // private
118     initEvents : function(){
119           
120         var allowed = "0123456789";
121         if(this.allowDecimals){
122             allowed += this.decimalSeparator;
123         }
124         allowed += this.thousandSeparator;
125         if(this.allowNegative){
126             allowed += "-";
127         }
128         this.stripCharsRe = new RegExp('[^'+allowed+']', 'gi');
129         var keyPress = function(e){
130             var k = e.getKey();
131             if(!Roo.isIE && (e.isSpecialKey() || k == e.BACKSPACE || k == e.DELETE)){
132                 return;
133             }
134             var c = e.getCharCode();
135             if(allowed.indexOf(String.fromCharCode(c)) === -1){
136                 e.stopEvent();
137             }
138         };
139         this.el.on("keypress", keyPress, this);
140     },
141
142     // private
143     validateValue : function(value){
144         if(!Roo.form.NumberField.superclass.validateValue.call(this, value)){
145             return false;
146         }
147         if(value.length < 1){ // if it's blank and textfield didn't flag it then it's valid
148              return true;
149         }
150         var num = this.parseValue(value);
151         if(isNaN(num)){
152             this.markInvalid(String.format(this.nanText, value));
153             return false;
154         }
155         if(num < this.minValue){
156             this.markInvalid(String.format(this.minText, this.minValue));
157             return false;
158         }
159         if(num > this.maxValue){
160             this.markInvalid(String.format(this.maxText, this.maxValue));
161             return false;
162         }
163         return true;
164     },
165
166     getValue : function(){
167         return this.fixPrecision(this.parseValue(Roo.form.NumberField.superclass.getValue.call(this)));
168     },
169
170     // private
171     parseValue : function(value){
172         value = parseFloat(String(value).replace(this.decimalSeparator, ".").split(this.thousandSeparator).join(''));
173         return isNaN(value) ? '' : value;
174     },
175
176     // private
177     fixPrecision : function(value){
178         var nan = isNaN(value);
179         if(!this.allowDecimals || this.decimalPrecision == -1 || nan || !value){
180             return nan ? '' : value;
181         }
182         return parseFloat(value).toFixed(this.decimalPrecision);
183     },
184
185     setValue : function(v){
186         v = this.fixPrecision(v);
187         if(this.thousandSeparator != ''){
188             v = Roo.util.Format.number(v, this.decimalPrecision, this.thousandSeparator);
189         } 
190         Roo.form.NumberField.superclass.setValue.call(this, String(v).replace(".", this.decimalSeparator));
191         if (this.hiddenField !== false) {
192             this.hiddenField.value = v ? this.parseValue(v) : '';
193         }
194         
195
196     },
197
198     // private
199     decimalPrecisionFcn : function(v){
200         return Math.floor(v);
201     },
202
203     beforeBlur : function(){
204         var v = this.parseValue(this.getRawValue());
205         if(v){
206             this.setValue(v);
207         }
208     }
209 });