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'>/**
19 </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);
107 * @docauthor Jason Johnston <jason@sencha.com>
109 Ext.define('Ext.form.field.Number', {
110 extend:'Ext.form.field.Spinner',
111 alias: 'widget.numberfield',
112 alternateClassName: ['Ext.form.NumberField', 'Ext.form.Number'],
114 <span id='Ext-form-field-Number-cfg-stripCharsRe'> /**
115 </span> * @cfg {RegExp} stripCharsRe @hide
117 <span id='Ext-form-field-Number-cfg-maskRe'> /**
118 </span> * @cfg {RegExp} maskRe @hide
121 <span id='Ext-form-field-Number-cfg-allowDecimals'> /**
122 </span> * @cfg {Boolean} allowDecimals False to disallow decimal values (defaults to true)
124 allowDecimals : true,
126 <span id='Ext-form-field-Number-cfg-decimalSeparator'> /**
127 </span> * @cfg {String} decimalSeparator Character(s) to allow as the decimal separator (defaults to '.')
129 decimalSeparator : '.',
131 <span id='Ext-form-field-Number-cfg-decimalPrecision'> /**
132 </span> * @cfg {Number} decimalPrecision The maximum precision to display after the decimal separator (defaults to 2)
134 decimalPrecision : 2,
136 <span id='Ext-form-field-Number-cfg-minValue'> /**
137 </span> * @cfg {Number} minValue The minimum allowed value (defaults to Number.NEGATIVE_INFINITY). Will be used by
138 * the field's validation logic, and for
139 * {@link Ext.form.field.Spinner#setSpinUpEnabled enabling/disabling the down spinner button}.
141 minValue: Number.NEGATIVE_INFINITY,
143 <span id='Ext-form-field-Number-cfg-maxValue'> /**
144 </span> * @cfg {Number} maxValue The maximum allowed value (defaults to Number.MAX_VALUE). Will be used by
145 * the field's validation logic, and for
146 * {@link Ext.form.field.Spinner#setSpinUpEnabled enabling/disabling the up spinner button}.
148 maxValue: Number.MAX_VALUE,
150 <span id='Ext-form-field-Number-cfg-step'> /**
151 </span> * @cfg {Number} step Specifies a numeric interval by which the field's value will be incremented or
152 * decremented when the user invokes the spinner. Defaults to <tt>1</tt>.
156 <span id='Ext-form-field-Number-cfg-minText'> /**
157 </span> * @cfg {String} minText Error text to display if the minimum value validation fails (defaults to 'The minimum
158 * value for this field is {minValue}')
160 minText : 'The minimum value for this field is {0}',
162 <span id='Ext-form-field-Number-cfg-maxText'> /**
163 </span> * @cfg {String} maxText Error text to display if the maximum value validation fails (defaults to 'The maximum
164 * value for this field is {maxValue}')
166 maxText : 'The maximum value for this field is {0}',
168 <span id='Ext-form-field-Number-cfg-nanText'> /**
169 </span> * @cfg {String} nanText Error text to display if the value is not a valid number. For example, this can happen
170 * if a valid character like '.' or '-' is left in the field with no number (defaults to '{value} is not a valid number')
172 nanText : '{0} is not a valid number',
174 <span id='Ext-form-field-Number-cfg-negativeText'> /**
175 </span> * @cfg {String} negativeText Error text to display if the value is negative and {@link #minValue} is set to
176 * <tt>0</tt>. This is used instead of the {@link #minText} in that circumstance only.
178 negativeText : 'The value cannot be negative',
180 <span id='Ext-form-field-Number-cfg-baseChars'> /**
181 </span> * @cfg {String} baseChars The base set of characters to evaluate as valid numbers (defaults to '0123456789').
183 baseChars : '0123456789',
185 <span id='Ext-form-field-Number-cfg-autoStripChars'> /**
186 </span> * @cfg {Boolean} autoStripChars True to automatically strip not allowed characters from the field. Defaults to <tt>false</tt>
188 autoStripChars: false,
190 initComponent: function() {
196 me.setMinValue(me.minValue);
197 me.setMaxValue(me.maxValue);
199 // Build regexes for masking and stripping based on the configured options
200 if (me.disableKeyFilter !== true) {
201 allowed = me.baseChars + '';
202 if (me.allowDecimals) {
203 allowed += me.decimalSeparator;
205 if (me.minValue < 0) {
208 allowed = Ext.String.escapeRegex(allowed);
209 me.maskRe = new RegExp('[' + allowed + ']');
210 if (me.autoStripChars) {
211 me.stripCharsRe = new RegExp('[^' + allowed + ']', 'gi');
216 <span id='Ext-form-field-Number-method-getErrors'> /**
217 </span> * Runs all of Number's validations and returns an array of any errors. Note that this first
218 * runs Text's validations, so the returned array is an amalgamation of all field errors.
219 * The additional validations run test that the value is a number, and that it is within the
220 * configured min and max values.
221 * @param {Mixed} value The value to get errors for (defaults to the current field value)
222 * @return {Array} All validation errors for this field
224 getErrors: function(value) {
226 errors = me.callParent(arguments),
227 format = Ext.String.format,
230 value = Ext.isDefined(value) ? value : this.processRawValue(this.getRawValue());
232 if (value.length < 1) { // if it's blank and textfield didn't flag it then it's valid
236 value = String(value).replace(me.decimalSeparator, '.');
239 errors.push(format(me.nanText, value));
242 num = me.parseValue(value);
244 if (me.minValue === 0 && num < 0) {
245 errors.push(this.negativeText);
247 else if (num < me.minValue) {
248 errors.push(format(me.minText, me.minValue));
251 if (num > me.maxValue) {
252 errors.push(format(me.maxText, me.maxValue));
259 rawToValue: function(rawValue) {
260 var value = this.fixPrecision(this.parseValue(rawValue));
261 if (value === null) {
262 value = rawValue || null;
267 valueToRaw: function(value) {
269 decimalSeparator = me.decimalSeparator;
270 value = me.parseValue(value);
271 value = me.fixPrecision(value);
272 value = Ext.isNumber(value) ? value : parseFloat(String(value).replace(decimalSeparator, '.'));
273 value = isNaN(value) ? '' : String(value).replace('.', decimalSeparator);
277 onChange: function() {
279 value = me.getValue(),
280 valueIsNull = value === null;
282 me.callParent(arguments);
284 // Update the spinner buttons
285 me.setSpinUpEnabled(valueIsNull || value < me.maxValue);
286 me.setSpinDownEnabled(valueIsNull || value > me.minValue);
289 <span id='Ext-form-field-Number-method-setMinValue'> /**
290 </span> * Replaces any existing {@link #minValue} with the new value.
291 * @param {Number} value The minimum value
293 setMinValue : function(value) {
294 this.minValue = Ext.Number.from(value, Number.NEGATIVE_INFINITY);
297 <span id='Ext-form-field-Number-method-setMaxValue'> /**
298 </span> * Replaces any existing {@link #maxValue} with the new value.
299 * @param {Number} value The maximum value
301 setMaxValue: function(value) {
302 this.maxValue = Ext.Number.from(value, Number.MAX_VALUE);
306 parseValue : function(value) {
307 value = parseFloat(String(value).replace(this.decimalSeparator, '.'));
308 return isNaN(value) ? null : value;
311 <span id='Ext-form-field-Number-method-fixPrecision'> /**
315 fixPrecision : function(value) {
318 precision = me.decimalPrecision;
321 return nan ? '' : value;
322 } else if (!me.allowDecimals || precision <= 0) {
326 return parseFloat(Ext.Number.toFixed(parseFloat(value), precision));
329 beforeBlur : function() {
331 v = me.parseValue(me.getRawValue());
333 if (!Ext.isEmpty(v)) {
338 onSpinUp: function() {
341 me.setValue(Ext.Number.constrain(me.getValue() + me.step, me.minValue, me.maxValue));
345 onSpinDown: function() {
348 me.setValue(Ext.Number.constrain(me.getValue() - me.step, me.minValue, me.maxValue));