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.3.1
11 * Copyright(c) 2006-2010 Sencha Inc.
12 * licensing@sencha.com
13 * http://www.sencha.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",
36 <div id="cfg-Ext.form.NumberField-allowDecimals"></div>/**
37 * @cfg {Boolean} allowDecimals False to disallow decimal values (defaults to true)
41 <div id="cfg-Ext.form.NumberField-decimalSeparator"></div>/**
42 * @cfg {String} decimalSeparator Character(s) to allow as the decimal separator (defaults to '.')
44 decimalSeparator : ".",
46 <div id="cfg-Ext.form.NumberField-decimalPrecision"></div>/**
47 * @cfg {Number} decimalPrecision The maximum precision to display after the decimal separator (defaults to 2)
51 <div id="cfg-Ext.form.NumberField-allowNegative"></div>/**
52 * @cfg {Boolean} allowNegative False to prevent entering a negative sign (defaults to true)
56 <div id="cfg-Ext.form.NumberField-minValue"></div>/**
57 * @cfg {Number} minValue The minimum allowed value (defaults to Number.NEGATIVE_INFINITY)
59 minValue : Number.NEGATIVE_INFINITY,
61 <div id="cfg-Ext.form.NumberField-maxValue"></div>/**
62 * @cfg {Number} maxValue The maximum allowed value (defaults to Number.MAX_VALUE)
64 maxValue : Number.MAX_VALUE,
66 <div id="cfg-Ext.form.NumberField-minText"></div>/**
67 * @cfg {String} minText Error text to display if the minimum value validation fails (defaults to "The minimum value for this field is {minValue}")
69 minText : "The minimum value for this field is {0}",
71 <div id="cfg-Ext.form.NumberField-maxText"></div>/**
72 * @cfg {String} maxText Error text to display if the maximum value validation fails (defaults to "The maximum value for this field is {maxValue}")
74 maxText : "The maximum value for this field is {0}",
76 <div id="cfg-Ext.form.NumberField-nanText"></div>/**
77 * @cfg {String} nanText Error text to display if the value is not a valid number. For example, this can happen
78 * if a valid character like '.' or '-' is left in the field with no number (defaults to "{value} is not a valid number")
80 nanText : "{0} is not a valid number",
82 <div id="cfg-Ext.form.NumberField-baseChars"></div>/**
83 * @cfg {String} baseChars The base set of characters to evaluate as valid numbers (defaults to '0123456789').
85 baseChars : "0123456789",
87 <div id="cfg-Ext.form.NumberField-autoStripChars"></div>/**
88 * @cfg {Boolean} autoStripChars True to automatically strip not allowed characters from the field. Defaults to <tt>false</tt>
90 autoStripChars: false,
93 initEvents : function() {
94 var allowed = this.baseChars + '';
95 if (this.allowDecimals) {
96 allowed += this.decimalSeparator;
98 if (this.allowNegative) {
101 allowed = Ext.escapeRe(allowed);
102 this.maskRe = new RegExp('[' + allowed + ']');
103 if (this.autoStripChars) {
104 this.stripCharsRe = new RegExp('[^' + allowed + ']', 'gi');
107 Ext.form.NumberField.superclass.initEvents.call(this);
110 <div id="method-Ext.form.NumberField-getErrors"></div>/**
111 * Runs all of NumberFields validations and returns an array of any errors. Note that this first
112 * runs TextField's validations, so the returned array is an amalgamation of all field errors.
113 * The additional validations run test that the value is a number, and that it is within the
114 * configured min and max values.
115 * @param {Mixed} value The value to get errors for (defaults to the current field value)
116 * @return {Array} All validation errors for this field
118 getErrors: function(value) {
119 var errors = Ext.form.NumberField.superclass.getErrors.apply(this, arguments);
121 value = Ext.isDefined(value) ? value : this.processValue(this.getRawValue());
123 if (value.length < 1) { // if it's blank and textfield didn't flag it then it's valid
127 value = String(value).replace(this.decimalSeparator, ".");
130 errors.push(String.format(this.nanText, value));
133 var num = this.parseValue(value);
135 if (num < this.minValue) {
136 errors.push(String.format(this.minText, this.minValue));
139 if (num > this.maxValue) {
140 errors.push(String.format(this.maxText, this.maxValue));
146 getValue : function() {
147 return this.fixPrecision(this.parseValue(Ext.form.NumberField.superclass.getValue.call(this)));
150 setValue : function(v) {
151 v = this.fixPrecision(v);
152 v = Ext.isNumber(v) ? v : parseFloat(String(v).replace(this.decimalSeparator, "."));
153 v = isNaN(v) ? '' : String(v).replace(".", this.decimalSeparator);
154 return Ext.form.NumberField.superclass.setValue.call(this, v);
157 <div id="method-Ext.form.NumberField-setMinValue"></div>/**
158 * Replaces any existing {@link #minValue} with the new value.
159 * @param {Number} value The minimum value
161 setMinValue : function(value) {
162 this.minValue = Ext.num(value, Number.NEGATIVE_INFINITY);
165 <div id="method-Ext.form.NumberField-setMaxValue"></div>/**
166 * Replaces any existing {@link #maxValue} with the new value.
167 * @param {Number} value The maximum value
169 setMaxValue : function(value) {
170 this.maxValue = Ext.num(value, Number.MAX_VALUE);
174 parseValue : function(value) {
175 value = parseFloat(String(value).replace(this.decimalSeparator, "."));
176 return isNaN(value) ? '' : value;
183 fixPrecision : function(value) {
184 var nan = isNaN(value);
186 if (!this.allowDecimals || this.decimalPrecision == -1 || nan || !value) {
187 return nan ? '' : value;
190 return parseFloat(parseFloat(value).toFixed(this.decimalPrecision));
193 beforeBlur : function() {
194 var v = this.parseValue(this.getRawValue());
196 if (!Ext.isEmpty(v)) {
202 Ext.reg('numberfield', Ext.form.NumberField);