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)
69 * @cfg {Boolean} allowThousandsDelimiter (true|false) display thousands delimiter if true (e.g. "100,000") (defalut false)
71 allowThousandsDelimiter : false,
73 * @cfg {String} symbol of thousandsDelimiter
75 thousandsDelimiter : ",",
77 getAutoCreate : function()
83 cls: 'hidden-number-input'
87 hiddenInput.name = this.name;
92 var cfg = Roo.bootstrap.NumberField.superclass.getAutoCreate.call(this);
94 if(cfg.cn.length > 0) {
95 cfg.cn.push(hiddenInput);
104 initEvents : function()
106 Roo.bootstrap.NumberField.superclass.initEvents.call(this);
108 var allowed = "0123456789";
110 if(this.allowDecimals){
111 allowed += this.decimalSeparator;
114 if(this.allowNegative){
118 this.stripCharsRe = new RegExp('[^'+allowed+']', 'gi');
120 var keyPress = function(e){
124 var c = e.getCharCode();
127 (String.fromCharCode(c) == '.' || String.fromCharCode(c) == '-') &&
128 allowed.indexOf(String.fromCharCode(c)) === -1
134 if(!Roo.isIE && (e.isSpecialKey() || k == e.BACKSPACE || k == e.DELETE)){
138 if(allowed.indexOf(String.fromCharCode(c)) === -1){
143 this.el.on("keypress", keyPress, this);
146 validateValue : function(value)
149 if(!Roo.bootstrap.NumberField.superclass.validateValue.call(this, value)){
153 var num = this.parseValue(value);
156 this.markInvalid(String.format(this.nanText, value));
160 if(num < this.minValue){
161 this.markInvalid(String.format(this.minText, this.minValue));
165 if(num > this.maxValue){
166 this.markInvalid(String.format(this.maxText, this.maxValue));
173 getValue : function()
175 return this.fixPrecision(this.parseValue(Roo.bootstrap.NumberField.superclass.getValue.call(this)));
178 parseValue : function(value)
180 value = parseFloat(String(value).replace(this.decimalSeparator, "."));
181 return isNaN(value) ? '' : value;
184 fixPrecision : function(value)
186 var nan = isNaN(value);
188 if(!this.allowDecimals || this.decimalPrecision == -1 || nan || !value){
189 return nan ? '' : value;
191 return parseFloat(value).toFixed(this.decimalPrecision);
194 setValue : function(v)
196 v = this.fixPrecision(v);
197 Roo.bootstrap.NumberField.superclass.setValue.call(this, String(v).replace(".", this.decimalSeparator));
200 decimalPrecisionFcn : function(v)
202 return Math.floor(v);
205 beforeBlur : function()
211 var v = this.parseValue(this.getRawValue());
217 addThousandsDelimiter : function(v)
219 if(!this.allowThousandsDelimiter) {
225 var x = v.split(".");
229 var x2 = x.length > 1 ? "." + x[1] : "";
231 var rgx = /(\d+)(\d{3})/;
233 while (rgx.test(x1)) {
234 x1 = x1.replace(rgx, "$1" + this.thousandsDelimiter + "$2");
240 hiddenEl : function()
242 return this.el.select('input.hidden-number-input',true).first();