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)
45 * @cfg {Boolean} allowNegative False to prevent entering a negative sign (defaults to true)
49 * @cfg {Number} minValue The minimum allowed value (defaults to Number.NEGATIVE_INFINITY)
51 minValue : Number.NEGATIVE_INFINITY,
53 * @cfg {Number} maxValue The maximum allowed value (defaults to Number.MAX_VALUE)
55 maxValue : Number.MAX_VALUE,
57 * @cfg {String} minText Error text to display if the minimum value validation fails (defaults to "The minimum value for this field is {minValue}")
59 minText : "The minimum value for this field is {0}",
61 * @cfg {String} maxText Error text to display if the maximum value validation fails (defaults to "The maximum value for this field is {maxValue}")
63 maxText : "The maximum value for this field is {0}",
65 * @cfg {String} nanText Error text to display if the value is not a valid number. For example, this can happen
66 * if a valid character like '.' or '-' is left in the field with no number (defaults to "{value} is not a valid number")
68 nanText : "{0} is not a valid number",
70 * @cfg {Boolean} castInt (true|false) cast int if true (defalut true)
74 * @cfg {String} thousandsDelimiter Symbol of thousandsDelimiter
76 thousandsDelimiter : false,
78 * @cfg {String} valueAlign alignment of value
82 getAutoCreate : function()
88 cls: 'hidden-number-input'
92 hiddenInput.name = this.name;
97 var cfg = Roo.bootstrap.NumberField.superclass.getAutoCreate.call(this);
99 this.name = hiddenInput.name;
101 if(cfg.cn.length > 0) {
102 cfg.cn.push(hiddenInput);
109 initEvents : function()
111 Roo.bootstrap.NumberField.superclass.initEvents.call(this);
113 var allowed = "0123456789";
115 if(this.allowDecimals){
116 allowed += this.decimalSeparator;
119 if(this.allowNegative){
123 if(this.thousandsDelimiter) {
127 this.stripCharsRe = new RegExp('[^'+allowed+']', 'gi');
129 var keyPress = function(e){
133 var c = e.getCharCode();
136 (String.fromCharCode(c) == '.' || String.fromCharCode(c) == '-') &&
137 allowed.indexOf(String.fromCharCode(c)) === -1
143 if(!Roo.isIE && (e.isSpecialKey() || k == e.BACKSPACE || k == e.DELETE)){
147 if(allowed.indexOf(String.fromCharCode(c)) === -1){
152 this.el.on("keypress", keyPress, this);
155 validateValue : function(value)
158 if(!Roo.bootstrap.NumberField.superclass.validateValue.call(this, value)){
162 var num = this.parseValue(value);
165 this.markInvalid(String.format(this.nanText, value));
169 if(num < this.minValue){
170 this.markInvalid(String.format(this.minText, this.minValue));
174 if(num > this.maxValue){
175 this.markInvalid(String.format(this.maxText, this.maxValue));
182 getValue : function()
184 var v = this.hiddenEl().getValue();
186 return this.fixPrecision(this.parseValue(v));
189 parseValue : function(value)
191 if(this.thousandsDelimiter) {
193 r = new RegExp(",", "g");
194 value = value.replace(r, "");
197 value = parseFloat(String(value).replace(this.decimalSeparator, "."));
198 return isNaN(value) ? '' : value;
201 fixPrecision : function(value)
203 if(this.thousandsDelimiter) {
205 r = new RegExp(",", "g");
206 value = value.replace(r, "");
209 var nan = isNaN(value);
211 if(!this.allowDecimals || this.decimalPrecision == -1 || nan || !value){
212 return nan ? '' : value;
214 return parseFloat(value).toFixed(this.decimalPrecision);
217 setValue : function(v)
219 v = String(this.fixPrecision(v)).replace(".", this.decimalSeparator);
225 this.hiddenEl().dom.value = (v === null || v === undefined ? '' : v);
227 this.inputEl().dom.value = v == '' ? '' :
228 Roo.util.Format.number(v, this.decimalPrecision, this.thousandsDelimiter || '');
230 if(!this.allowZero && !v) {
231 this.hiddenEl().dom.value = '';
232 this.inputEl().dom.value = '';
239 decimalPrecisionFcn : function(v)
241 return Math.floor(v);
244 beforeBlur : function()
250 var v = this.parseValue(this.getRawValue());
257 hiddenEl : function()
259 return this.el.select('input.hidden-number-input',true).first();