4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../resources/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> * @docauthor Jason Johnston <jason@sencha.com>
21 * A numeric text field that provides automatic keystroke filtering to disallow non-numeric characters,
22 * and numeric validation to limit the value to a range of valid numbers. The range of acceptable number
23 * values can be controlled by setting the {@link #minValue} and {@link #maxValue} configs, and fractional
24 * decimals can be disallowed by setting {@link #allowDecimals} to `false`.
26 * By default, the number field is also rendered with a set of up/down spinner buttons and has
27 * up/down arrow key and mouse wheel event listeners attached for incrementing/decrementing the value by the
28 * {@link #step} value. To hide the spinner buttons set `{@link #hideTrigger hideTrigger}:true`; to disable
29 * the arrow key and mouse wheel handlers set `{@link #keyNavEnabled keyNavEnabled}:false` and
30 * `{@link #mouseWheelEnabled mouseWheelEnabled}:false`. See the example below.
35 * Ext.create('Ext.form.Panel', {
36 * title: 'On The Wall',
39 * renderTo: Ext.getBody(),
41 * xtype: 'numberfield',
44 * fieldLabel: 'Bottles of Beer',
50 * text: 'Take one down, pass it around',
51 * handler: function() {
52 * this.up('form').down('[name=bottles]').spinDown();
57 * # Removing UI Enhancements
60 * Ext.create('Ext.form.Panel', {
61 * title: 'Personal Info',
64 * renderTo: Ext.getBody(),
66 * xtype: 'numberfield',
70 * minValue: 0, //prevents negative numbers
72 * // Remove spinner buttons, and arrow key and mouse wheel listeners
74 * keyNavEnabled: false,
75 * mouseWheelEnabled: false
82 * Ext.create('Ext.form.Panel', {
83 * renderTo: Ext.getBody(),
88 * xtype: 'numberfield',
91 * fieldLabel: 'Even Numbers',
93 * // Set step so it skips every other number
97 * // Add change handler to force user-entered numbers to evens
99 * change: function(field, value) {
100 * value = parseInt(value, 10);
101 * field.setValue(value + value % 2);
107 Ext.define('Ext.form.field.Number', {
108 extend:'Ext.form.field.Spinner',
109 alias: 'widget.numberfield',
110 alternateClassName: ['Ext.form.NumberField', 'Ext.form.Number'],
112 <span id='Ext-form-field-Number-cfg-stripCharsRe'> /**
113 </span> * @cfg {RegExp} stripCharsRe @hide
115 <span id='Ext-form-field-Number-cfg-maskRe'> /**
116 </span> * @cfg {RegExp} maskRe @hide
119 <span id='Ext-form-field-Number-cfg-allowDecimals'> /**
120 </span> * @cfg {Boolean} allowDecimals
121 * False to disallow decimal values
123 allowDecimals : true,
125 <span id='Ext-form-field-Number-cfg-decimalSeparator'> /**
126 </span> * @cfg {String} decimalSeparator
127 * Character(s) to allow as the decimal separator
129 decimalSeparator : '.',
131 <span id='Ext-form-field-Number-cfg-decimalPrecision'> /**
132 </span> * @cfg {Number} decimalPrecision
133 * The maximum precision to display after the decimal separator
135 decimalPrecision : 2,
137 <span id='Ext-form-field-Number-cfg-minValue'> /**
138 </span> * @cfg {Number} minValue
139 * The minimum allowed value (defaults to Number.NEGATIVE_INFINITY). Will be used by the field's validation logic,
140 * and for {@link Ext.form.field.Spinner#setSpinUpEnabled enabling/disabling the down spinner button}.
142 minValue: Number.NEGATIVE_INFINITY,
144 <span id='Ext-form-field-Number-cfg-maxValue'> /**
145 </span> * @cfg {Number} maxValue
146 * The maximum allowed value (defaults to Number.MAX_VALUE). Will be used by the field's validation logic, and for
147 * {@link Ext.form.field.Spinner#setSpinUpEnabled enabling/disabling the up spinner button}.
149 maxValue: Number.MAX_VALUE,
151 <span id='Ext-form-field-Number-cfg-step'> /**
152 </span> * @cfg {Number} step
153 * Specifies a numeric interval by which the field's value will be incremented or decremented when the user invokes
158 <span id='Ext-form-field-Number-cfg-minText'> /**
159 </span> * @cfg {String} minText
160 * Error text to display if the minimum value validation fails.
162 minText : 'The minimum value for this field is {0}',
164 <span id='Ext-form-field-Number-cfg-maxText'> /**
165 </span> * @cfg {String} maxText
166 * Error text to display if the maximum value validation fails.
168 maxText : 'The maximum value for this field is {0}',
170 <span id='Ext-form-field-Number-cfg-nanText'> /**
171 </span> * @cfg {String} nanText
172 * Error text to display if the value is not a valid number. For example, this can happen if a valid character like
173 * '.' or '-' is left in the field with no number.
175 nanText : '{0} is not a valid number',
177 <span id='Ext-form-field-Number-cfg-negativeText'> /**
178 </span> * @cfg {String} negativeText
179 * Error text to display if the value is negative and {@link #minValue} is set to 0. This is used instead of the
180 * {@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
186 * The base set of characters to evaluate as valid numbers.
188 baseChars : '0123456789',
190 <span id='Ext-form-field-Number-cfg-autoStripChars'> /**
191 </span> * @cfg {Boolean} autoStripChars
192 * True to automatically strip not allowed characters from the field.
194 autoStripChars: false,
196 initComponent: function() {
202 me.setMinValue(me.minValue);
203 me.setMaxValue(me.maxValue);
205 // Build regexes for masking and stripping based on the configured options
206 if (me.disableKeyFilter !== true) {
207 allowed = me.baseChars + '';
208 if (me.allowDecimals) {
209 allowed += me.decimalSeparator;
211 if (me.minValue < 0) {
214 allowed = Ext.String.escapeRegex(allowed);
215 me.maskRe = new RegExp('[' + allowed + ']');
216 if (me.autoStripChars) {
217 me.stripCharsRe = new RegExp('[^' + allowed + ']', 'gi');
222 <span id='Ext-form-field-Number-method-getErrors'> /**
223 </span> * Runs all of Number's validations and returns an array of any errors. Note that this first runs Text's
224 * validations, so the returned array is an amalgamation of all field errors. The additional validations run test
225 * that the value is a number, and that it is within the configured min and max values.
226 * @param {Object} [value] The value to get errors for (defaults to the current field value)
227 * @return {String[]} All validation errors for this field
229 getErrors: function(value) {
231 errors = me.callParent(arguments),
232 format = Ext.String.format,
235 value = Ext.isDefined(value) ? value : this.processRawValue(this.getRawValue());
237 if (value.length < 1) { // if it's blank and textfield didn't flag it then it's valid
241 value = String(value).replace(me.decimalSeparator, '.');
244 errors.push(format(me.nanText, value));
247 num = me.parseValue(value);
249 if (me.minValue === 0 && num < 0) {
250 errors.push(this.negativeText);
252 else if (num < me.minValue) {
253 errors.push(format(me.minText, me.minValue));
256 if (num > me.maxValue) {
257 errors.push(format(me.maxText, me.maxValue));
264 rawToValue: function(rawValue) {
265 var value = this.fixPrecision(this.parseValue(rawValue));
266 if (value === null) {
267 value = rawValue || null;
272 valueToRaw: function(value) {
274 decimalSeparator = me.decimalSeparator;
275 value = me.parseValue(value);
276 value = me.fixPrecision(value);
277 value = Ext.isNumber(value) ? value : parseFloat(String(value).replace(decimalSeparator, '.'));
278 value = isNaN(value) ? '' : String(value).replace('.', decimalSeparator);
282 onChange: function() {
284 value = me.getValue(),
285 valueIsNull = value === null;
287 me.callParent(arguments);
289 // Update the spinner buttons
290 me.setSpinUpEnabled(valueIsNull || value < me.maxValue);
291 me.setSpinDownEnabled(valueIsNull || value > me.minValue);
294 <span id='Ext-form-field-Number-method-setMinValue'> /**
295 </span> * Replaces any existing {@link #minValue} with the new value.
296 * @param {Number} value The minimum value
298 setMinValue : function(value) {
299 this.minValue = Ext.Number.from(value, Number.NEGATIVE_INFINITY);
302 <span id='Ext-form-field-Number-method-setMaxValue'> /**
303 </span> * Replaces any existing {@link #maxValue} with the new value.
304 * @param {Number} value The maximum value
306 setMaxValue: function(value) {
307 this.maxValue = Ext.Number.from(value, Number.MAX_VALUE);
311 parseValue : function(value) {
312 value = parseFloat(String(value).replace(this.decimalSeparator, '.'));
313 return isNaN(value) ? null : value;
316 <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));