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