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>
8 <body onload="prettyPrint();">
9 <pre class="prettyprint lang-js">/*!
10 * Ext JS Library 3.2.1
11 * Copyright(c) 2006-2010 Ext JS, Inc.
13 * http://www.extjs.com/license
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.
20 * Creates a new NumberField
21 * @param {Object} config Configuration options
24 Ext.form.NumberField = Ext.extend(Ext.form.TextField, {
25 <div id="cfg-Ext.form.NumberField-stripCharsRe"></div>/**
26 * @cfg {RegExp} stripCharsRe @hide
28 <div id="cfg-Ext.form.NumberField-maskRe"></div>/**
29 * @cfg {RegExp} maskRe @hide
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")
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)
39 <div id="cfg-Ext.form.NumberField-decimalSeparator"></div>/**
40 * @cfg {String} decimalSeparator Character(s) to allow as the decimal separator (defaults to '.')
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)
47 <div id="cfg-Ext.form.NumberField-allowNegative"></div>/**
48 * @cfg {Boolean} allowNegative False to prevent entering a negative sign (defaults to true)
51 <div id="cfg-Ext.form.NumberField-minValue"></div>/**
52 * @cfg {Number} minValue The minimum allowed value (defaults to Number.NEGATIVE_INFINITY)
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)
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}")
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}")
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")
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').
75 baseChars : "0123456789",
78 initEvents : function(){
79 var allowed = this.baseChars + '';
80 if (this.allowDecimals) {
81 allowed += this.decimalSeparator;
83 if (this.allowNegative) {
86 this.maskRe = new RegExp('[' + Ext.escapeRe(allowed) + ']');
87 Ext.form.NumberField.superclass.initEvents.call(this);
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
98 getErrors: function(value) {
99 var errors = Ext.form.NumberField.superclass.getErrors.apply(this, arguments);
101 value = value || this.processValue(this.getRawValue());
103 if (value.length < 1) { // if it's blank and textfield didn't flag it then it's valid
107 value = String(value).replace(this.decimalSeparator, ".");
110 errors.push(String.format(this.nanText, value));
113 var num = this.parseValue(value);
115 if(num < this.minValue){
116 errors.push(String.format(this.minText, this.minValue));
119 if(num > this.maxValue){
120 errors.push(String.format(this.maxText, this.maxValue));
126 getValue : function(){
127 return this.fixPrecision(this.parseValue(Ext.form.NumberField.superclass.getValue.call(this)));
130 setValue : function(v){
131 v = Ext.isNumber(v) ? v : parseFloat(String(v).replace(this.decimalSeparator, "."));
132 v = isNaN(v) ? '' : String(v).replace(".", this.decimalSeparator);
133 return Ext.form.NumberField.superclass.setValue.call(this, v);
136 <div id="method-Ext.form.NumberField-setMinValue"></div>/**
137 * Replaces any existing {@link #minValue} with the new value.
138 * @param {Number} value The minimum value
140 setMinValue : function(value){
141 this.minValue = Ext.num(value, Number.NEGATIVE_INFINITY);
144 <div id="method-Ext.form.NumberField-setMaxValue"></div>/**
145 * Replaces any existing {@link #maxValue} with the new value.
146 * @param {Number} value The maximum value
148 setMaxValue : function(value){
149 this.maxValue = Ext.num(value, Number.MAX_VALUE);
153 parseValue : function(value){
154 value = parseFloat(String(value).replace(this.decimalSeparator, "."));
155 return isNaN(value) ? '' : value;
159 fixPrecision : function(value){
160 var nan = isNaN(value);
161 if(!this.allowDecimals || this.decimalPrecision == -1 || nan || !value){
162 return nan ? '' : value;
164 return parseFloat(parseFloat(value).toFixed(this.decimalPrecision));
167 beforeBlur : function(){
168 var v = this.parseValue(this.getRawValue());
170 this.setValue(this.fixPrecision(v));
174 Ext.reg('numberfield', Ext.form.NumberField);</pre>