3 <title>The source code</title>
\r
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
7 <body onload="prettyPrint();">
\r
8 <pre class="prettyprint lang-js"><div id="cls-Ext.form.NumberField"></div>/**
9 * @class Ext.form.NumberField
10 * @extends Ext.form.TextField
11 * Numeric text field that provides automatic keystroke filtering and numeric validation.
13 * Creates a new NumberField
14 * @param {Object} config Configuration options
17 Ext.form.NumberField = Ext.extend(Ext.form.TextField, {
18 <div id="cfg-Ext.form.NumberField-stripCharsRe"></div>/**
19 * @cfg {RegExp} stripCharsRe @hide
21 <div id="cfg-Ext.form.NumberField-maskRe"></div>/**
22 * @cfg {RegExp} maskRe @hide
24 <div id="cfg-Ext.form.NumberField-fieldClass"></div>/**
25 * @cfg {String} fieldClass The default CSS class for the field (defaults to "x-form-field x-form-num-field")
27 fieldClass: "x-form-field x-form-num-field",
28 <div id="cfg-Ext.form.NumberField-allowDecimals"></div>/**
29 * @cfg {Boolean} allowDecimals False to disallow decimal values (defaults to true)
32 <div id="cfg-Ext.form.NumberField-decimalSeparator"></div>/**
33 * @cfg {String} decimalSeparator Character(s) to allow as the decimal separator (defaults to '.')
35 decimalSeparator : ".",
36 <div id="cfg-Ext.form.NumberField-decimalPrecision"></div>/**
37 * @cfg {Number} decimalPrecision The maximum precision to display after the decimal separator (defaults to 2)
40 <div id="cfg-Ext.form.NumberField-allowNegative"></div>/**
41 * @cfg {Boolean} allowNegative False to prevent entering a negative sign (defaults to true)
44 <div id="cfg-Ext.form.NumberField-minValue"></div>/**
45 * @cfg {Number} minValue The minimum allowed value (defaults to Number.NEGATIVE_INFINITY)
47 minValue : Number.NEGATIVE_INFINITY,
48 <div id="cfg-Ext.form.NumberField-maxValue"></div>/**
49 * @cfg {Number} maxValue The maximum allowed value (defaults to Number.MAX_VALUE)
51 maxValue : Number.MAX_VALUE,
52 <div id="cfg-Ext.form.NumberField-minText"></div>/**
53 * @cfg {String} minText Error text to display if the minimum value validation fails (defaults to "The minimum value for this field is {minValue}")
55 minText : "The minimum value for this field is {0}",
56 <div id="cfg-Ext.form.NumberField-maxText"></div>/**
57 * @cfg {String} maxText Error text to display if the maximum value validation fails (defaults to "The maximum value for this field is {maxValue}")
59 maxText : "The maximum value for this field is {0}",
60 <div id="cfg-Ext.form.NumberField-nanText"></div>/**
61 * @cfg {String} nanText Error text to display if the value is not a valid number. For example, this can happen
62 * if a valid character like '.' or '-' is left in the field with no number (defaults to "{value} is not a valid number")
64 nanText : "{0} is not a valid number",
65 <div id="cfg-Ext.form.NumberField-baseChars"></div>/**
66 * @cfg {String} baseChars The base set of characters to evaluate as valid numbers (defaults to '0123456789').
68 baseChars : "0123456789",
71 initEvents : function(){
72 var allowed = this.baseChars + '';
73 if (this.allowDecimals) {
74 allowed += this.decimalSeparator;
76 if (this.allowNegative) {
79 this.maskRe = new RegExp('[' + Ext.escapeRe(allowed) + ']');
80 Ext.form.NumberField.superclass.initEvents.call(this);
84 validateValue : function(value){
85 if(!Ext.form.NumberField.superclass.validateValue.call(this, value)){
88 if(value.length < 1){ // if it's blank and textfield didn't flag it then it's valid
91 value = String(value).replace(this.decimalSeparator, ".");
93 this.markInvalid(String.format(this.nanText, value));
96 var num = this.parseValue(value);
97 if(num < this.minValue){
98 this.markInvalid(String.format(this.minText, this.minValue));
101 if(num > this.maxValue){
102 this.markInvalid(String.format(this.maxText, this.maxValue));
108 getValue : function(){
109 return this.fixPrecision(this.parseValue(Ext.form.NumberField.superclass.getValue.call(this)));
112 setValue : function(v){
113 v = typeof v == 'number' ? v : parseFloat(String(v).replace(this.decimalSeparator, "."));
114 v = isNaN(v) ? '' : String(v).replace(".", this.decimalSeparator);
115 return Ext.form.NumberField.superclass.setValue.call(this, v);
119 parseValue : function(value){
120 value = parseFloat(String(value).replace(this.decimalSeparator, "."));
121 return isNaN(value) ? '' : value;
125 fixPrecision : function(value){
126 var nan = isNaN(value);
127 if(!this.allowDecimals || this.decimalPrecision == -1 || nan || !value){
128 return nan ? '' : value;
130 return parseFloat(parseFloat(value).toFixed(this.decimalPrecision));
133 beforeBlur : function(){
134 var v = this.parseValue(this.getRawValue());
136 this.setValue(this.fixPrecision(v));
140 Ext.reg('numberfield', Ext.form.NumberField);</pre>
\r