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()
79 var hidden_input_name = this.name;
85 var cfg = Roo.bootstrap.NumberField.superclass.getAutoCreate.call(this);
87 if(cfg.cn.length > 0) {
90 name : hidden_input_name,
93 cls: 'form-hidden-field'
103 initEvents : function()
105 Roo.bootstrap.NumberField.superclass.initEvents.call(this);
107 var allowed = "0123456789";
109 if(this.allowDecimals){
110 allowed += this.decimalSeparator;
113 if(this.allowNegative){
117 this.stripCharsRe = new RegExp('[^'+allowed+']', 'gi');
119 var keyPress = function(e){
123 var c = e.getCharCode();
126 (String.fromCharCode(c) == '.' || String.fromCharCode(c) == '-') &&
127 allowed.indexOf(String.fromCharCode(c)) === -1
133 if(!Roo.isIE && (e.isSpecialKey() || k == e.BACKSPACE || k == e.DELETE)){
137 if(allowed.indexOf(String.fromCharCode(c)) === -1){
142 this.el.on("keypress", keyPress, this);
145 validateValue : function(value)
148 if(!Roo.bootstrap.NumberField.superclass.validateValue.call(this, value)){
152 var num = this.parseValue(value);
155 this.markInvalid(String.format(this.nanText, value));
159 if(num < this.minValue){
160 this.markInvalid(String.format(this.minText, this.minValue));
164 if(num > this.maxValue){
165 this.markInvalid(String.format(this.maxText, this.maxValue));
172 getValue : function()
174 return this.fixPrecision(this.parseValue(Roo.bootstrap.NumberField.superclass.getValue.call(this)));
177 parseValue : function(value)
179 value = parseFloat(String(value).replace(this.decimalSeparator, "."));
180 return isNaN(value) ? '' : value;
183 fixPrecision : function(value)
185 var nan = isNaN(value);
187 if(!this.allowDecimals || this.decimalPrecision == -1 || nan || !value){
188 return nan ? '' : value;
190 return parseFloat(value).toFixed(this.decimalPrecision);
193 setValue : function(v)
195 v = this.fixPrecision(v);
196 Roo.bootstrap.NumberField.superclass.setValue.call(this, String(v).replace(".", this.decimalSeparator));
199 decimalPrecisionFcn : function(v)
201 return Math.floor(v);
204 beforeBlur : function()
210 var v = this.parseValue(this.getRawValue());
216 addThousandsDelimiter : function(v)
218 if(!this.allowThousandsDelimiter) {
224 var x = v.split(".");
228 var x2 = x.length > 1 ? "." + x[1] : "";
230 var rgx = /(\d+)(\d{3})/;
232 while (rgx.test(x1)) {
233 x1 = x1.replace(rgx, "$1" + this.thousandsDelimiter + "$2");
241 return this.el.select('input.form-hidden-field',true).first();