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;
83 var cfg = Roo.bootstrap.NumberField.superclass.getAutoCreate.call(this);
85 if(cfg.cn.length > 0) {
88 name : hidden_input_name,
90 cls: 'form-hidden-field-number'
98 initEvents : function()
100 Roo.bootstrap.NumberField.superclass.initEvents.call(this);
102 var allowed = "0123456789";
104 if(this.allowDecimals){
105 allowed += this.decimalSeparator;
108 if(this.allowNegative){
112 this.stripCharsRe = new RegExp('[^'+allowed+']', 'gi');
114 var keyPress = function(e){
118 var c = e.getCharCode();
121 (String.fromCharCode(c) == '.' || String.fromCharCode(c) == '-') &&
122 allowed.indexOf(String.fromCharCode(c)) === -1
128 if(!Roo.isIE && (e.isSpecialKey() || k == e.BACKSPACE || k == e.DELETE)){
132 if(allowed.indexOf(String.fromCharCode(c)) === -1){
137 this.el.on("keypress", keyPress, this);
140 validateValue : function(value)
143 if(!Roo.bootstrap.NumberField.superclass.validateValue.call(this, value)){
147 var num = this.parseValue(value);
150 this.markInvalid(String.format(this.nanText, value));
154 if(num < this.minValue){
155 this.markInvalid(String.format(this.minText, this.minValue));
159 if(num > this.maxValue){
160 this.markInvalid(String.format(this.maxText, this.maxValue));
167 getValue : function()
169 return this.fixPrecision(this.parseValue(Roo.bootstrap.NumberField.superclass.getValue.call(this)));
172 parseValue : function(value)
174 value = parseFloat(String(value).replace(this.decimalSeparator, "."));
175 return isNaN(value) ? '' : value;
178 fixPrecision : function(value)
180 var nan = isNaN(value);
182 if(!this.allowDecimals || this.decimalPrecision == -1 || nan || !value){
183 return nan ? '' : value;
185 return parseFloat(value).toFixed(this.decimalPrecision);
188 setValue : function(v)
190 v = this.fixPrecision(v);
191 Roo.bootstrap.NumberField.superclass.setValue.call(this, String(v).replace(".", this.decimalSeparator));
194 decimalPrecisionFcn : function(v)
196 return Math.floor(v);
199 beforeBlur : function()
205 var v = this.parseValue(this.getRawValue());
211 addThousandsDelimiter : function(v)
213 if(!this.allowThousandsDelimiter) {
219 var x = v.split(".");
223 var x2 = x.length > 1 ? "." + x[1] : "";
225 var rgx = /(\d+)(\d{3})/;
227 while (rgx.test(x1)) {
228 x1 = x1.replace(rgx, "$1" + this.thousandsDelimiter + "$2");
236 Roo.log('die in here???');
238 return this.el.select('input.form-hidden-field-number',true).first();