3 <title>The source code</title>
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 <body onload="prettyPrint();">
8 <pre class="prettyprint lang-js">/*!
10 * Copyright(c) 2006-2009 Ext JS, LLC
12 * http://www.extjs.com/license
14 <div id="cls-Ext.form.NumberField"></div>/**
15 * @class Ext.form.NumberField
16 * @extends Ext.form.TextField
17 * Numeric text field that provides automatic keystroke filtering and numeric validation.
19 * Creates a new NumberField
20 * @param {Object} config Configuration options
23 Ext.form.NumberField = Ext.extend(Ext.form.TextField, {
24 <div id="cfg-Ext.form.NumberField-stripCharsRe"></div>/**
25 * @cfg {RegExp} stripCharsRe @hide
27 <div id="cfg-Ext.form.NumberField-maskRe"></div>/**
28 * @cfg {RegExp} maskRe @hide
30 <div id="cfg-Ext.form.NumberField-fieldClass"></div>/**
31 * @cfg {String} fieldClass The default CSS class for the field (defaults to "x-form-field x-form-num-field")
33 fieldClass: "x-form-field x-form-num-field",
34 <div id="cfg-Ext.form.NumberField-allowDecimals"></div>/**
35 * @cfg {Boolean} allowDecimals False to disallow decimal values (defaults to true)
38 <div id="cfg-Ext.form.NumberField-decimalSeparator"></div>/**
39 * @cfg {String} decimalSeparator Character(s) to allow as the decimal separator (defaults to '.')
41 decimalSeparator : ".",
42 <div id="cfg-Ext.form.NumberField-decimalPrecision"></div>/**
43 * @cfg {Number} decimalPrecision The maximum precision to display after the decimal separator (defaults to 2)
46 <div id="cfg-Ext.form.NumberField-allowNegative"></div>/**
47 * @cfg {Boolean} allowNegative False to prevent entering a negative sign (defaults to true)
50 <div id="cfg-Ext.form.NumberField-minValue"></div>/**
51 * @cfg {Number} minValue The minimum allowed value (defaults to Number.NEGATIVE_INFINITY)
53 minValue : Number.NEGATIVE_INFINITY,
54 <div id="cfg-Ext.form.NumberField-maxValue"></div>/**
55 * @cfg {Number} maxValue The maximum allowed value (defaults to Number.MAX_VALUE)
57 maxValue : Number.MAX_VALUE,
58 <div id="cfg-Ext.form.NumberField-minText"></div>/**
59 * @cfg {String} minText Error text to display if the minimum value validation fails (defaults to "The minimum value for this field is {minValue}")
61 minText : "The minimum value for this field is {0}",
62 <div id="cfg-Ext.form.NumberField-maxText"></div>/**
63 * @cfg {String} maxText Error text to display if the maximum value validation fails (defaults to "The maximum value for this field is {maxValue}")
65 maxText : "The maximum value for this field is {0}",
66 <div id="cfg-Ext.form.NumberField-nanText"></div>/**
67 * @cfg {String} nanText Error text to display if the value is not a valid number. For example, this can happen
68 * if a valid character like '.' or '-' is left in the field with no number (defaults to "{value} is not a valid number")
70 nanText : "{0} is not a valid number",
71 <div id="cfg-Ext.form.NumberField-baseChars"></div>/**
72 * @cfg {String} baseChars The base set of characters to evaluate as valid numbers (defaults to '0123456789').
74 baseChars : "0123456789",
77 initEvents : function(){
78 var allowed = this.baseChars + '';
79 if (this.allowDecimals) {
80 allowed += this.decimalSeparator;
82 if (this.allowNegative) {
85 this.maskRe = new RegExp('[' + Ext.escapeRe(allowed) + ']');
86 Ext.form.NumberField.superclass.initEvents.call(this);
90 validateValue : function(value){
91 if(!Ext.form.NumberField.superclass.validateValue.call(this, value)){
94 if(value.length < 1){ // if it's blank and textfield didn't flag it then it's valid
97 value = String(value).replace(this.decimalSeparator, ".");
99 this.markInvalid(String.format(this.nanText, value));
102 var num = this.parseValue(value);
103 if(num < this.minValue){
104 this.markInvalid(String.format(this.minText, this.minValue));
107 if(num > this.maxValue){
108 this.markInvalid(String.format(this.maxText, this.maxValue));
114 getValue : function(){
115 return this.fixPrecision(this.parseValue(Ext.form.NumberField.superclass.getValue.call(this)));
118 setValue : function(v){
119 v = typeof v == 'number' ? v : parseFloat(String(v).replace(this.decimalSeparator, "."));
120 v = isNaN(v) ? '' : String(v).replace(".", this.decimalSeparator);
121 return Ext.form.NumberField.superclass.setValue.call(this, v);
125 parseValue : function(value){
126 value = parseFloat(String(value).replace(this.decimalSeparator, "."));
127 return isNaN(value) ? '' : value;
131 fixPrecision : function(value){
132 var nan = isNaN(value);
133 if(!this.allowDecimals || this.decimalPrecision == -1 || nan || !value){
134 return nan ? '' : value;
136 return parseFloat(parseFloat(value).toFixed(this.decimalPrecision));
139 beforeBlur : function(){
140 var v = this.parseValue(this.getRawValue());
142 this.setValue(this.fixPrecision(v));
146 Ext.reg('numberfield', Ext.form.NumberField);</pre>