3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
\r
4 <title>The source code</title>
\r
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
8 <body onload="prettyPrint();">
\r
9 <pre class="prettyprint lang-js"><div id="cls-Ext.form.NumberField"></div>/**
10 * @class Ext.form.NumberField
11 * @extends Ext.form.TextField
12 * Numeric text field that provides automatic keystroke filtering and numeric validation.
14 * Creates a new NumberField
15 * @param {Object} config Configuration options
18 Ext.form.NumberField = Ext.extend(Ext.form.TextField, {
19 <div id="cfg-Ext.form.NumberField-stripCharsRe"></div>/**
20 * @cfg {RegExp} stripCharsRe @hide
22 <div id="cfg-Ext.form.NumberField-maskRe"></div>/**
23 * @cfg {RegExp} maskRe @hide
25 <div id="cfg-Ext.form.NumberField-fieldClass"></div>/**
26 * @cfg {String} fieldClass The default CSS class for the field (defaults to "x-form-field x-form-num-field")
28 fieldClass: "x-form-field x-form-num-field",
29 <div id="cfg-Ext.form.NumberField-allowDecimals"></div>/**
30 * @cfg {Boolean} allowDecimals False to disallow decimal values (defaults to true)
33 <div id="cfg-Ext.form.NumberField-decimalSeparator"></div>/**
34 * @cfg {String} decimalSeparator Character(s) to allow as the decimal separator (defaults to '.')
36 decimalSeparator : ".",
37 <div id="cfg-Ext.form.NumberField-decimalPrecision"></div>/**
38 * @cfg {Number} decimalPrecision The maximum precision to display after the decimal separator (defaults to 2)
41 <div id="cfg-Ext.form.NumberField-allowNegative"></div>/**
42 * @cfg {Boolean} allowNegative False to prevent entering a negative sign (defaults to true)
45 <div id="cfg-Ext.form.NumberField-minValue"></div>/**
46 * @cfg {Number} minValue The minimum allowed value (defaults to Number.NEGATIVE_INFINITY)
48 minValue : Number.NEGATIVE_INFINITY,
49 <div id="cfg-Ext.form.NumberField-maxValue"></div>/**
50 * @cfg {Number} maxValue The maximum allowed value (defaults to Number.MAX_VALUE)
52 maxValue : Number.MAX_VALUE,
53 <div id="cfg-Ext.form.NumberField-minText"></div>/**
54 * @cfg {String} minText Error text to display if the minimum value validation fails (defaults to "The minimum value for this field is {minValue}")
56 minText : "The minimum value for this field is {0}",
57 <div id="cfg-Ext.form.NumberField-maxText"></div>/**
58 * @cfg {String} maxText Error text to display if the maximum value validation fails (defaults to "The maximum value for this field is {maxValue}")
60 maxText : "The maximum value for this field is {0}",
61 <div id="cfg-Ext.form.NumberField-nanText"></div>/**
62 * @cfg {String} nanText Error text to display if the value is not a valid number. For example, this can happen
63 * if a valid character like '.' or '-' is left in the field with no number (defaults to "{value} is not a valid number")
65 nanText : "{0} is not a valid number",
66 <div id="cfg-Ext.form.NumberField-baseChars"></div>/**
67 * @cfg {String} baseChars The base set of characters to evaluate as valid numbers (defaults to '0123456789').
69 baseChars : "0123456789",
72 initEvents : function(){
73 var allowed = this.baseChars + '';
74 if (this.allowDecimals) {
75 allowed += this.decimalSeparator;
77 if (this.allowNegative) {
80 this.maskRe = new RegExp('[' + Ext.escapeRe(allowed) + ']');
81 Ext.form.NumberField.superclass.initEvents.call(this);
85 validateValue : function(value){
86 if(!Ext.form.NumberField.superclass.validateValue.call(this, value)){
89 if(value.length < 1){ // if it's blank and textfield didn't flag it then it's valid
92 value = String(value).replace(this.decimalSeparator, ".");
94 this.markInvalid(String.format(this.nanText, value));
97 var num = this.parseValue(value);
98 if(num < this.minValue){
99 this.markInvalid(String.format(this.minText, this.minValue));
102 if(num > this.maxValue){
103 this.markInvalid(String.format(this.maxText, this.maxValue));
109 getValue : function(){
110 return this.fixPrecision(this.parseValue(Ext.form.NumberField.superclass.getValue.call(this)));
113 setValue : function(v){
114 v = Ext.isNumber(v) ? v : parseFloat(String(v).replace(this.decimalSeparator, "."));
115 v = isNaN(v) ? '' : String(v).replace(".", this.decimalSeparator);
116 return Ext.form.NumberField.superclass.setValue.call(this, v);
119 <div id="method-Ext.form.NumberField-setMinValue"></div>/**
120 * Replaces any existing {@link #minValue} with the new value.
121 * @param {Number} value The minimum value
123 setMinValue : function(value){
124 this.minValue = Ext.num(value, Number.NEGATIVE_INFINITY);
127 <div id="method-Ext.form.NumberField-setMaxValue"></div>/**
128 * Replaces any existing {@link #maxValue} with the new value.
129 * @param {Number} value The maximum value
131 setMaxValue : function(value){
132 this.maxValue = Ext.num(value, Number.MAX_VALUE);
136 parseValue : function(value){
137 value = parseFloat(String(value).replace(this.decimalSeparator, "."));
138 return isNaN(value) ? '' : value;
142 fixPrecision : function(value){
143 var nan = isNaN(value);
144 if(!this.allowDecimals || this.decimalPrecision == -1 || nan || !value){
145 return nan ? '' : value;
147 return parseFloat(parseFloat(value).toFixed(this.decimalPrecision));
150 beforeBlur : function(){
151 var v = this.parseValue(this.getRawValue());
153 this.setValue(this.fixPrecision(v));
157 Ext.reg('numberfield', Ext.form.NumberField);</pre>
\r