9 * @class Roo.bootstrap.NumberField
10 * @extends Roo.bootstrap.Input
11 * Bootstrap NumberField class
17 * Create a new NumberField
18 * @param {Object} config The config object
21 Roo.bootstrap.NumberField = function(config){
22 Roo.bootstrap.NumberField.superclass.constructor.call(this, config);
25 Roo.extend(Roo.bootstrap.NumberField, Roo.bootstrap.Input, {
28 * @cfg {Boolean} allowDecimals False to disallow decimal values (defaults to true)
32 * @cfg {String} decimalSeparator Character(s) to allow as the decimal separator (defaults to '.')
34 decimalSeparator : ".",
36 * @cfg {Number} decimalPrecision The maximum precision to display after the decimal separator (defaults to 2)
40 * @cfg {Boolean} allowNegative False to prevent entering a negative sign (defaults to true)
44 * @cfg {Number} minValue The minimum allowed value (defaults to Number.NEGATIVE_INFINITY)
46 minValue : Number.NEGATIVE_INFINITY,
48 * @cfg {Number} maxValue The maximum allowed value (defaults to Number.MAX_VALUE)
50 maxValue : Number.MAX_VALUE,
52 * @cfg {String} minText Error text to display if the minimum value validation fails (defaults to "The minimum value for this field is {minValue}")
54 minText : "The minimum value for this field is {0}",
56 * @cfg {String} maxText Error text to display if the maximum value validation fails (defaults to "The maximum value for this field is {maxValue}")
58 maxText : "The maximum value for this field is {0}",
60 * @cfg {String} nanText Error text to display if the value is not a valid number. For example, this can happen
61 * if a valid character like '.' or '-' is left in the field with no number (defaults to "{value} is not a valid number")
63 nanText : "{0} is not a valid number",
65 * @cfg {Boolean} castInt (true|false) cast int if true (defalut true)
70 initEvents : function()
72 Roo.bootstrap.NumberField.superclass.initEvents.call(this);
74 var allowed = "0123456789";
76 if(this.allowDecimals){
77 allowed += this.decimalSeparator;
80 if(this.allowNegative){
84 this.stripCharsRe = new RegExp('[^'+allowed+']', 'gi');
86 var keyPress = function(e){
90 var c = e.getCharCode();
93 (String.fromCharCode(c) == '.' || String.fromCharCode(c) == '-') &&
94 allowed.indexOf(String.fromCharCode(c)) === -1
100 if(!Roo.isIE && (e.isSpecialKey() || k == e.BACKSPACE || k == e.DELETE)){
104 if(allowed.indexOf(String.fromCharCode(c)) === -1){
109 this.el.on("keypress", keyPress, this);
112 validateValue : function(value)
115 if(!Roo.bootstrap.NumberField.superclass.validateValue.call(this, value)){
119 var num = this.parseValue(value);
122 this.markInvalid(String.format(this.nanText, value));
126 if(num < this.minValue){
127 this.markInvalid(String.format(this.minText, this.minValue));
131 if(num > this.maxValue){
132 this.markInvalid(String.format(this.maxText, this.maxValue));
139 getValue : function()
141 return this.fixPrecision(this.parseValue(Roo.bootstrap.NumberField.superclass.getValue.call(this)));
144 parseValue : function(value)
146 value = parseFloat(String(value).replace(this.decimalSeparator, "."));
147 return isNaN(value) ? '' : value;
150 fixPrecision : function(value)
152 var nan = isNaN(value);
154 if(!this.allowDecimals || this.decimalPrecision == -1 || nan || !value){
155 return nan ? '' : value;
157 return parseFloat(value).toFixed(this.decimalPrecision);
160 setValue : function(v)
162 v = this.fixPrecision(v);
163 Roo.bootstrap.NumberField.superclass.setValue.call(this, String(v).replace(".", this.decimalSeparator));
166 decimalPrecisionFcn : function(v)
168 return Math.floor(v);
171 beforeBlur : function()
177 var v = this.parseValue(this.getRawValue());