4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
14 * @class Roo.form.NumberField
15 * @extends Roo.form.TextField
16 * Numeric text field that provides automatic keystroke filtering and numeric validation.
18 * Creates a new NumberField
19 * @param {Object} config Configuration options
21 Roo.form.NumberField = function(config){
22 Roo.form.NumberField.superclass.constructor.call(this, config);
25 Roo.extend(Roo.form.NumberField, Roo.form.TextField, {
27 * @cfg {String} fieldClass The default CSS class for the field (defaults to "x-form-field x-form-num-field")
29 fieldClass: "x-form-field x-form-num-field",
31 * @cfg {Boolean} allowDecimals False to disallow decimal values (defaults to true)
35 * @cfg {String} decimalSeparator Character(s) to allow as the decimal separator (defaults to '.')
37 decimalSeparator : ".",
39 * @cfg {String} thousandSeparator Character(s) to allow as the thousand separator (defaults to '') - set to ',' for example
41 thousandSeparator : "",
43 * @cfg {Number} decimalPrecision The maximum precision to display after the decimal separator (defaults to 2)
47 * @cfg {Boolean} allowNegative False to prevent entering a negative sign (defaults to true)
51 * @cfg {Number} minValue The minimum allowed value (defaults to Number.NEGATIVE_INFINITY)
53 minValue : Number.NEGATIVE_INFINITY,
55 * @cfg {Number} maxValue The maximum allowed value (defaults to Number.MAX_VALUE)
57 maxValue : Number.MAX_VALUE,
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}",
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}",
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",
73 initEvents : function(){
74 Roo.form.NumberField.superclass.initEvents.call(this);
75 var allowed = "0123456789";
76 if(this.allowDecimals){
77 allowed += this.decimalSeparator;
79 allowed += this.thousandSeparator;
80 if(this.allowNegative){
83 this.stripCharsRe = new RegExp('[^'+allowed+']', 'gi');
84 var keyPress = function(e){
86 if(!Roo.isIE && (e.isSpecialKey() || k == e.BACKSPACE || k == e.DELETE)){
89 var c = e.getCharCode();
90 if(allowed.indexOf(String.fromCharCode(c)) === -1){
94 this.el.on("keypress", keyPress, this);
98 validateValue : function(value){
99 if(!Roo.form.NumberField.superclass.validateValue.call(this, value)){
102 if(value.length < 1){ // if it's blank and textfield didn't flag it then it's valid
105 var num = this.parseValue(value);
107 this.markInvalid(String.format(this.nanText, value));
110 if(num < this.minValue){
111 this.markInvalid(String.format(this.minText, this.minValue));
114 if(num > this.maxValue){
115 this.markInvalid(String.format(this.maxText, this.maxValue));
121 getValue : function(){
122 return this.fixPrecision(this.parseValue(Roo.form.NumberField.superclass.getValue.call(this)));
126 parseValue : function(value){
127 value = parseFloat(String(value).replace(this.decimalSeparator, ".").replace(this.thousandSeparator, ''));
128 return isNaN(value) ? '' : value;
132 fixPrecision : function(value){
133 var nan = isNaN(value);
134 if(!this.allowDecimals || this.decimalPrecision == -1 || nan || !value){
135 return nan ? '' : value;
137 return parseFloat(value).toFixed(this.decimalPrecision);
140 setValue : function(v){
141 v = this.fixPrecision(v);
142 if(this.thousandSeparator != ''){
143 // v = Roo.util.Format.number(v, this.decimalPrecision, this.thousandSeparator);
145 Roo.form.NumberField.superclass.setValue.call(this, String(v).replace(".", this.decimalSeparator));
149 decimalPrecisionFcn : function(v){
150 return Math.floor(v);
153 beforeBlur : function(){
154 var v = this.parseValue(this.getRawValue());