4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-form-field-Number-method-constructor'><span id='Ext-form-field-Number'>/**
19 </span></span> * @class Ext.form.field.Number
20 * @extends Ext.form.field.Spinner
22 A numeric text field that provides automatic keystroke filtering to disallow non-numeric characters,
23 and numeric validation to limit the value to a range of valid numbers. The range of acceptable number
24 values can be controlled by setting the {@link #minValue} and {@link #maxValue} configs, and fractional
25 decimals can be disallowed by setting {@link #allowDecimals} to `false`.
27 By default, the number field is also rendered with a set of up/down spinner buttons and has
28 up/down arrow key and mouse wheel event listeners attached for incrementing/decrementing the value by the
29 {@link #step} value. To hide the spinner buttons set `{@link #hideTrigger hideTrigger}:true`; to disable the arrow key
30 and mouse wheel handlers set `{@link #keyNavEnabled keyNavEnabled}:false` and
31 `{@link #mouseWheelEnabled mouseWheelEnabled}:false`. See the example below.
34 {@img Ext.form.Number/Ext.form.Number1.png Ext.form.Number component}
35 Ext.create('Ext.form.Panel', {
39 renderTo: Ext.getBody(),
44 fieldLabel: 'Bottles of Beer',
50 text: 'Take one down, pass it around',
52 this.up('form').down('[name=bottles]').spinDown();
57 #Removing UI Enhancements#
58 {@img Ext.form.Number/Ext.form.Number2.png Ext.form.Number component}
59 Ext.create('Ext.form.Panel', {
60 title: 'Personal Info',
63 renderTo: Ext.getBody(),
69 minValue: 0, //prevents negative numbers
71 // Remove spinner buttons, and arrow key and mouse wheel listeners
74 mouseWheelEnabled: false
79 Ext.create('Ext.form.Panel', {
80 renderTo: Ext.getBody(),
88 fieldLabel: 'Even Numbers',
90 // Set step so it skips every other number
94 // Add change handler to force user-entered numbers to evens
96 change: function(field, value) {
97 value = parseInt(value, 10);
98 field.setValue(value + value % 2);
106 * Creates a new Number field
107 * @param {Object} config Configuration options
111 * @docauthor Jason Johnston <jason@sencha.com>
113 Ext.define('Ext.form.field.Number', {
114 extend:'Ext.form.field.Spinner',
115 alias: 'widget.numberfield',
116 alternateClassName: ['Ext.form.NumberField', 'Ext.form.Number'],
118 <span id='Ext-form-field-Number-cfg-stripCharsRe'> /**
119 </span> * @cfg {RegExp} stripCharsRe @hide
121 <span id='Ext-form-field-Number-cfg-maskRe'> /**
122 </span> * @cfg {RegExp} maskRe @hide
125 <span id='Ext-form-field-Number-cfg-allowDecimals'> /**
126 </span> * @cfg {Boolean} allowDecimals False to disallow decimal values (defaults to true)
128 allowDecimals : true,
130 <span id='Ext-form-field-Number-cfg-decimalSeparator'> /**
131 </span> * @cfg {String} decimalSeparator Character(s) to allow as the decimal separator (defaults to '.')
133 decimalSeparator : '.',
135 <span id='Ext-form-field-Number-cfg-decimalPrecision'> /**
136 </span> * @cfg {Number} decimalPrecision The maximum precision to display after the decimal separator (defaults to 2)
138 decimalPrecision : 2,
140 <span id='Ext-form-field-Number-cfg-minValue'> /**
141 </span> * @cfg {Number} minValue The minimum allowed value (defaults to Number.NEGATIVE_INFINITY). Will be used by
142 * the field's validation logic, and for
143 * {@link Ext.form.field.Spinner#setSpinUpEnabled enabling/disabling the down spinner button}.
145 minValue: Number.NEGATIVE_INFINITY,
147 <span id='Ext-form-field-Number-cfg-maxValue'> /**
148 </span> * @cfg {Number} maxValue The maximum allowed value (defaults to Number.MAX_VALUE). Will be used by
149 * the field's validation logic, and for
150 * {@link Ext.form.field.Spinner#setSpinUpEnabled enabling/disabling the up spinner button}.
152 maxValue: Number.MAX_VALUE,
154 <span id='Ext-form-field-Number-cfg-step'> /**
155 </span> * @cfg {Number} step Specifies a numeric interval by which the field's value will be incremented or
156 * decremented when the user invokes the spinner. Defaults to <tt>1</tt>.
160 <span id='Ext-form-field-Number-cfg-minText'> /**
161 </span> * @cfg {String} minText Error text to display if the minimum value validation fails (defaults to 'The minimum
162 * value for this field is {minValue}')
164 minText : 'The minimum value for this field is {0}',
166 <span id='Ext-form-field-Number-cfg-maxText'> /**
167 </span> * @cfg {String} maxText Error text to display if the maximum value validation fails (defaults to 'The maximum
168 * value for this field is {maxValue}')
170 maxText : 'The maximum value for this field is {0}',
172 <span id='Ext-form-field-Number-cfg-nanText'> /**
173 </span> * @cfg {String} nanText Error text to display if the value is not a valid number. For example, this can happen
174 * if a valid character like '.' or '-' is left in the field with no number (defaults to '{value} is not a valid number')
176 nanText : '{0} is not a valid number',
178 <span id='Ext-form-field-Number-cfg-negativeText'> /**
179 </span> * @cfg {String} negativeText Error text to display if the value is negative and {@link #minValue} is set to
180 * <tt>0</tt>. This is used instead of the {@link #minText} in that circumstance only.
182 negativeText : 'The value cannot be negative',
184 <span id='Ext-form-field-Number-cfg-baseChars'> /**
185 </span> * @cfg {String} baseChars The base set of characters to evaluate as valid numbers (defaults to '0123456789').
187 baseChars : '0123456789',
189 <span id='Ext-form-field-Number-cfg-autoStripChars'> /**
190 </span> * @cfg {Boolean} autoStripChars True to automatically strip not allowed characters from the field. Defaults to <tt>false</tt>
192 autoStripChars: false,
194 initComponent: function() {
200 me.setMinValue(me.minValue);
201 me.setMaxValue(me.maxValue);
203 // Build regexes for masking and stripping based on the configured options
204 if (me.disableKeyFilter !== true) {
205 allowed = me.baseChars + '';
206 if (me.allowDecimals) {
207 allowed += me.decimalSeparator;
209 if (me.minValue < 0) {
212 allowed = Ext.String.escapeRegex(allowed);
213 me.maskRe = new RegExp('[' + allowed + ']');
214 if (me.autoStripChars) {
215 me.stripCharsRe = new RegExp('[^' + allowed + ']', 'gi');
220 <span id='Ext-form-field-Number-method-getErrors'> /**
221 </span> * Runs all of Number's validations and returns an array of any errors. Note that this first
222 * runs Text's validations, so the returned array is an amalgamation of all field errors.
223 * The additional validations run test that the value is a number, and that it is within the
224 * configured min and max values.
225 * @param {Mixed} value The value to get errors for (defaults to the current field value)
226 * @return {Array} All validation errors for this field
228 getErrors: function(value) {
230 errors = me.callParent(arguments),
231 format = Ext.String.format,
234 value = Ext.isDefined(value) ? value : this.processRawValue(this.getRawValue());
236 if (value.length < 1) { // if it's blank and textfield didn't flag it then it's valid
240 value = String(value).replace(me.decimalSeparator, '.');
243 errors.push(format(me.nanText, value));
246 num = me.parseValue(value);
248 if (me.minValue === 0 && num < 0) {
249 errors.push(this.negativeText);
251 else if (num < me.minValue) {
252 errors.push(format(me.minText, me.minValue));
255 if (num > me.maxValue) {
256 errors.push(format(me.maxText, me.maxValue));
263 rawToValue: function(rawValue) {
264 var value = this.fixPrecision(this.parseValue(rawValue));
265 if (value === null) {
266 value = rawValue || null;
271 valueToRaw: function(value) {
273 decimalSeparator = me.decimalSeparator;
274 value = me.parseValue(value);
275 value = me.fixPrecision(value);
276 value = Ext.isNumber(value) ? value : parseFloat(String(value).replace(decimalSeparator, '.'));
277 value = isNaN(value) ? '' : String(value).replace('.', decimalSeparator);
281 onChange: function() {
283 value = me.getValue(),
284 valueIsNull = value === null;
286 me.callParent(arguments);
288 // Update the spinner buttons
289 me.setSpinUpEnabled(valueIsNull || value < me.maxValue);
290 me.setSpinDownEnabled(valueIsNull || value > me.minValue);
293 <span id='Ext-form-field-Number-method-setMinValue'> /**
294 </span> * Replaces any existing {@link #minValue} with the new value.
295 * @param {Number} value The minimum value
297 setMinValue : function(value) {
298 this.minValue = Ext.Number.from(value, Number.NEGATIVE_INFINITY);
301 <span id='Ext-form-field-Number-method-setMaxValue'> /**
302 </span> * Replaces any existing {@link #maxValue} with the new value.
303 * @param {Number} value The maximum value
305 setMaxValue: function(value) {
306 this.maxValue = Ext.Number.from(value, Number.MAX_VALUE);
310 parseValue : function(value) {
311 value = parseFloat(String(value).replace(this.decimalSeparator, '.'));
312 return isNaN(value) ? null : value;
315 <span id='Ext-form-field-Number-method-fixPrecision'> /**
319 fixPrecision : function(value) {
322 precision = me.decimalPrecision;
325 return nan ? '' : value;
326 } else if (!me.allowDecimals || precision <= 0) {
330 return parseFloat(Ext.Number.toFixed(parseFloat(value), precision));
333 beforeBlur : function() {
335 v = me.parseValue(me.getRawValue());
337 if (!Ext.isEmpty(v)) {
342 onSpinUp: function() {
345 me.setValue(Ext.Number.constrain(me.getValue() + me.step, me.minValue, me.maxValue));
349 onSpinDown: function() {
352 me.setValue(Ext.Number.constrain(me.getValue() - me.step, me.minValue, me.maxValue));