8 * @class Roo.bootstrap.NumberField
9 * @extends Roo.bootstrap.Input
10 * Bootstrap NumberField class
16 * Create a new NumberField
17 * @param {Object} config The config object
20 Roo.bootstrap.NumberField = function(config){
21 Roo.bootstrap.NumberField.superclass.constructor.call(this, config);
24 Roo.extend(Roo.bootstrap.NumberField, Roo.bootstrap.Input, {
27 * @cfg {Boolean} allowDecimals False to disallow decimal values (defaults to true)
31 * @cfg {String} decimalSeparator Character(s) to allow as the decimal separator (defaults to '.')
33 decimalSeparator : ".",
35 * @cfg {Number} decimalPrecision The maximum precision to display after the decimal separator (defaults to 2)
39 * @cfg {Boolean} allowNegative False to prevent entering a negative sign (defaults to true)
44 * @cfg {Boolean} allowZero False to blank out if the user enters '0' (defaults to true)
48 * @cfg {Number} minValue The minimum allowed value (defaults to Number.NEGATIVE_INFINITY)
50 minValue : Number.NEGATIVE_INFINITY,
52 * @cfg {Number} maxValue The maximum allowed value (defaults to Number.MAX_VALUE)
54 maxValue : Number.MAX_VALUE,
56 * @cfg {String} minText Error text to display if the minimum value validation fails (defaults to "The minimum value for this field is {minValue}")
58 minText : "The minimum value for this field is {0}",
60 * @cfg {String} maxText Error text to display if the maximum value validation fails (defaults to "The maximum value for this field is {maxValue}")
62 maxText : "The maximum value for this field is {0}",
64 * @cfg {String} nanText Error text to display if the value is not a valid number. For example, this can happen
65 * if a valid character like '.' or '-' is left in the field with no number (defaults to "{value} is not a valid number")
67 nanText : "{0} is not a valid number",
69 * @cfg {Boolean} castInt (true|false) cast int if true (defalut true)
73 * @cfg {String} thousandsDelimiter Symbol of thousandsDelimiter
75 thousandsDelimiter : false,
77 * @cfg {String} valueAlign alignment of value
81 getAutoCreate : function()
87 cls: 'hidden-number-input'
91 hiddenInput.name = this.name;
96 var cfg = Roo.bootstrap.NumberField.superclass.getAutoCreate.call(this);
98 this.name = hiddenInput.name;
100 if(cfg.cn.length > 0) {
101 cfg.cn.push(hiddenInput);
108 initEvents : function()
110 Roo.bootstrap.NumberField.superclass.initEvents.call(this);
112 var allowed = "0123456789";
114 if(this.allowDecimals){
115 allowed += this.decimalSeparator;
118 if(this.allowNegative){
122 if(this.thousandsDelimiter) {
126 this.stripCharsRe = new RegExp('[^'+allowed+']', 'gi');
128 var keyPress = function(e){
132 var c = e.getCharCode();
135 (String.fromCharCode(c) == '.' || String.fromCharCode(c) == '-') &&
136 allowed.indexOf(String.fromCharCode(c)) === -1
142 if(!Roo.isIE && (e.isSpecialKey() || k == e.BACKSPACE || k == e.DELETE)){
146 if(allowed.indexOf(String.fromCharCode(c)) === -1){
151 this.el.on("keypress", keyPress, this);
154 validateValue : function(value)
157 if(!Roo.bootstrap.NumberField.superclass.validateValue.call(this, value)){
161 var num = this.parseValue(value);
164 this.markInvalid(String.format(this.nanText, value));
168 if(num < this.minValue){
169 this.markInvalid(String.format(this.minText, this.minValue));
173 if(num > this.maxValue){
174 this.markInvalid(String.format(this.maxText, this.maxValue));
181 getValue : function()
183 var v = this.hiddenEl().getValue();
185 return this.fixPrecision(this.parseValue(v));
188 parseValue : function(value)
190 if(this.thousandsDelimiter) {
192 r = new RegExp(",", "g");
193 value = value.replace(r, "");
196 value = parseFloat(String(value).replace(this.decimalSeparator, "."));
197 return isNaN(value) ? '' : value;
200 fixPrecision : function(value)
202 if(this.thousandsDelimiter) {
204 r = new RegExp(",", "g");
205 value = value.replace(r, "");
208 var nan = isNaN(value);
210 if(!this.allowDecimals || this.decimalPrecision == -1 || nan || !value){
211 return nan ? '' : value;
213 return parseFloat(value).toFixed(this.decimalPrecision);
216 setValue : function(v)
218 v = String(this.fixPrecision(v)).replace(".", this.decimalSeparator);
224 this.hiddenEl().dom.value = (v === null || v === undefined ? '' : v);
226 this.inputEl().dom.value = (v == '') ? '' :
227 Roo.util.Format.number(v, this.decimalPrecision, this.thousandsDelimiter || '');
231 if(!this.allowZero && v === '0') {
232 this.hiddenEl().dom.value = '';
233 this.inputEl().dom.value = '';
240 decimalPrecisionFcn : function(v)
242 return Math.floor(v);
245 beforeBlur : function()
251 var v = this.parseValue(this.getRawValue());
258 hiddenEl : function()
260 return this.el.select('input.hidden-number-input',true).first();