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} thousandsDelimiter Symbol of thousandsDelimiter
75 thousandsDelimiter : ",",
77 * @cfg {String} valueAlign alignment of value
81 getAutoCreate : function()
87 cls: 'hidden-number-input'
91 hiddenInput.name = this.name;
95 var inputConfig = this;
97 inputConfig.name = '';
99 var cfg = Roo.bootstrap.NumberField.superclass.getAutoCreate.call(this);
101 this.name = hiddenInput.name;
103 if(cfg.cn.length > 0) {
104 cfg.cn.push(hiddenInput);
111 initEvents : function()
113 Roo.bootstrap.NumberField.superclass.initEvents.call(this);
115 var allowed = "0123456789";
117 if(this.allowDecimals){
118 allowed += this.decimalSeparator;
121 if(this.allowNegative){
125 if(this.allowThousandsDelimiter) {
126 allowed += this.thousandsDelimiter;
129 this.stripCharsRe = new RegExp('[^'+allowed+']', 'gi');
131 var keyPress = function(e){
135 var c = e.getCharCode();
138 (String.fromCharCode(c) == '.' || String.fromCharCode(c) == '-') &&
139 allowed.indexOf(String.fromCharCode(c)) === -1
145 if(!Roo.isIE && (e.isSpecialKey() || k == e.BACKSPACE || k == e.DELETE)){
149 if(allowed.indexOf(String.fromCharCode(c)) === -1){
154 this.el.on("keypress", keyPress, this);
157 validateValue : function(value)
160 if(!Roo.bootstrap.NumberField.superclass.validateValue.call(this, value)){
164 var num = this.parseValue(value);
167 this.markInvalid(String.format(this.nanText, value));
171 if(num < this.minValue){
172 this.markInvalid(String.format(this.minText, this.minValue));
176 if(num > this.maxValue){
177 this.markInvalid(String.format(this.maxText, this.maxValue));
184 getValue : function()
186 var v = this.hiddenEl().getValue();
188 return this.fixPrecision(this.parseValue(v));
191 parseValue : function(value)
193 if(this.allowThousandsDelimiter) {
195 r = new RegExp(this.thousandsDelimiter, "g");
196 value = value.replace(r, "");
199 value = parseFloat(String(value).replace(this.decimalSeparator, "."));
200 return isNaN(value) ? '' : value;
203 fixPrecision : function(value)
205 if(this.allowThousandsDelimiter) {
207 r = new RegExp(this.thousandsDelimiter, "g");
208 value = value.replace(r, "");
211 var nan = isNaN(value);
213 if(!this.allowDecimals || this.decimalPrecision == -1 || nan || !value){
214 return nan ? '' : value;
216 return parseFloat(value).toFixed(this.decimalPrecision);
219 setValue : function(v)
221 v = String(this.fixPrecision(v)).replace(".", this.decimalSeparator);
227 this.hiddenEl().dom.value = (v === null || v === undefined ? '' : v);
229 this.inputEl().dom.value = (this.allowThousandsDelimiter ? this.addThousandsDelimiter(v) : v);
235 decimalPrecisionFcn : function(v)
237 return Math.floor(v);
240 beforeBlur : function()
246 var v = this.parseValue(this.getRawValue());
252 addThousandsDelimiter : function(v)
254 if(!this.allowThousandsDelimiter) {
260 var x = v.split(".");
264 var x2 = x.length > 1 ? "." + x[1] : "";
266 var rgx = /(\d+)(\d{3})/;
268 while (rgx.test(x1)) {
269 x1 = x1.replace(rgx, "$1" + this.thousandsDelimiter + "$2");
275 hiddenEl : function()
277 return this.el.select('input.hidden-number-input',true).first();