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 {String} thousandsDelimiter Symbol of thousandsDelimiter
71 thousandsDelimiter : false,
73 * @cfg {String} valueAlign alignment of value
77 getAutoCreate : function()
83 cls: 'hidden-number-input'
87 hiddenInput.name = this.name;
92 var cfg = Roo.bootstrap.NumberField.superclass.getAutoCreate.call(this);
94 this.name = hiddenInput.name;
96 if(cfg.cn.length > 0) {
97 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 if(this.thousandsDelimiter) {
122 this.stripCharsRe = new RegExp('[^'+allowed+']', 'gi');
124 var keyPress = function(e){
128 var c = e.getCharCode();
131 (String.fromCharCode(c) == '.' || String.fromCharCode(c) == '-') &&
132 allowed.indexOf(String.fromCharCode(c)) === -1
138 if(!Roo.isIE && (e.isSpecialKey() || k == e.BACKSPACE || k == e.DELETE)){
142 if(allowed.indexOf(String.fromCharCode(c)) === -1){
147 this.el.on("keypress", keyPress, this);
150 validateValue : function(value)
153 if(!Roo.bootstrap.NumberField.superclass.validateValue.call(this, value)){
157 var num = this.parseValue(value);
160 this.markInvalid(String.format(this.nanText, value));
164 if(num < this.minValue){
165 this.markInvalid(String.format(this.minText, this.minValue));
169 if(num > this.maxValue){
170 this.markInvalid(String.format(this.maxText, this.maxValue));
177 getValue : function()
179 var v = this.hiddenEl().getValue();
181 return this.fixPrecision(this.parseValue(v));
184 parseValue : function(value)
186 if(this.thousandsDelimiter) {
188 r = new RegExp(",", "g");
189 value = value.replace(r, "");
192 value = parseFloat(String(value).replace(this.decimalSeparator, "."));
193 return isNaN(value) ? '' : value;
196 fixPrecision : function(value)
198 if(this.thousandsDelimiter) {
200 r = new RegExp(",", "g");
201 value = value.replace(r, "");
204 var nan = isNaN(value);
206 if(!this.allowDecimals || this.decimalPrecision == -1 || nan || !value){
207 return nan ? '' : value;
209 return parseFloat(value).toFixed(this.decimalPrecision);
212 setValue : function(v)
214 v = String(this.fixPrecision(v)).replace(".", this.decimalSeparator);
220 this.hiddenEl().dom.value = (v === null || v === undefined ? '' : v);
222 this.inputEl().dom.value = Roo.util.Format.number(v, this.decimalPrecision,
223 this.thousandsDelimiter || ''
226 if(this.allowBlank && !v) {
227 this.inputEl().dom.value = '';
234 decimalPrecisionFcn : function(v)
236 return Math.floor(v);
239 beforeBlur : function()
245 var v = this.parseValue(this.getRawValue());
251 hiddenEl : function()
253 return this.el.select('input.hidden-number-input',true).first();