/* * Based on: * Ext JS Library 1.1.1 * Copyright(c) 2006-2007, Ext JS, LLC. * * Originally Released Under LGPL - original licence link has changed is not relivant. * * Fork - LGPL * <script type="text/javascript"> */ /** * @class Roo.form.NumberField * @extends Roo.form.TextField * Numeric text field that provides automatic keystroke filtering and numeric validation. * @constructor * Creates a new NumberField * @param {Object} config Configuration options */ Roo.form.NumberField = function(config){ Roo.form.NumberField.superclass.constructor.call(this, config); }; Roo.extend(Roo.form.NumberField, Roo.form.TextField, { /** * @cfg {String} fieldClass The default CSS class for the field (defaults to "x-form-field x-form-num-field") */ fieldClass: "x-form-field x-form-num-field", /** * @cfg {Boolean} allowDecimals False to disallow decimal values (defaults to true) */ allowDecimals : true, /** * @cfg {String} decimalSeparator Character(s) to allow as the decimal separator (defaults to '.') */ decimalSeparator : ".", /** * @cfg {String} thousandSeparator Character(s) to allow as the thousand separator (defaults to '') - set to ',' for example */ thousandSeparator : "", /** * @cfg {Number} decimalPrecision The maximum precision to display after the decimal separator (defaults to 2) */ decimalPrecision : 2, /** * @cfg {Boolean} allowNegative False to prevent entering a negative sign (defaults to true) */ allowNegative : true, /** * @cfg {Number} minValue The minimum allowed value (defaults to Number.NEGATIVE_INFINITY) */ minValue : Number.NEGATIVE_INFINITY, /** * @cfg {Number} maxValue The maximum allowed value (defaults to Number.MAX_VALUE) */ maxValue : Number.MAX_VALUE, /** * @cfg {String} minText Error text to display if the minimum value validation fails (defaults to "The minimum value for this field is {minValue}") */ minText : "The minimum value for this field is {0}", /** * @cfg {String} maxText Error text to display if the maximum value validation fails (defaults to "The maximum value for this field is {maxValue}") */ maxText : "The maximum value for this field is {0}", /** * @cfg {String} nanText Error text to display if the value is not a valid number. For example, this can happen * if a valid character like '.' or '-' is left in the field with no number (defaults to "{value} is not a valid number") */ nanText : "{0} is not a valid number", hiddenField : false, onRender : function(ct, position) { Roo.form.TextField.superclass.onRender.call(this, ct, position); //this.el.dom.removeAttribute('name'); Roo.log("Changing name?"); if (this.thousandSeparator != '') { this.el.dom.setAttribute('name', this.name + '____hidden___' ); this.hiddenField = this.el.insertSibling({ tag:'input', type:'hidden', name: this.name }, 'before', true); this.hiddenField.value = this.value ? this.parseValue(this.value) : ''; this.el.on('blur', this.onBlur, this); } // prevent input submission }, onBlur : function(){ this.beforeBlur(); if(!Roo.isOpera && this.focusClass){ // don't touch in Opera this.el.removeClass(this.focusClass); } this.hasFocus = false; if(this.validationEvent !== false && this.validateOnBlur && this.validationEvent != "blur"){ this.validate(); } var v = this.getValue(); if(String(v) !== String(this.startValue)){ this.setValue( this.parseValue(v)); this.fireEvent('change', this, v, this.startValue); } this.fireEvent("blur", this); }, // override name, so that it works with hidden field. getName: function(){ if (this.thousandSeparator != '') { return this.name; } return Roo.form.TextField.superclass.getName.call(this); }, // private initEvents : function(){ var allowed = "0123456789"; if(this.allowDecimals){ allowed += this.decimalSeparator; } allowed += this.thousandSeparator; if(this.allowNegative){ allowed += "-"; } this.stripCharsRe = new RegExp('[^'+allowed+']', 'gi'); var keyPress = function(e){ var k = e.getKey(); if(!Roo.isIE && (e.isSpecialKey() || k == e.BACKSPACE || k == e.DELETE)){ return; } var c = e.getCharCode(); if(allowed.indexOf(String.fromCharCode(c)) === -1){ e.stopEvent(); } }; this.el.on("keypress", keyPress, this); }, // private validateValue : function(value){ if(!Roo.form.NumberField.superclass.validateValue.call(this, value)){ return false; } if(value.length < 1){ // if it's blank and textfield didn't flag it then it's valid return true; } var num = this.parseValue(value); if(isNaN(num)){ this.markInvalid(String.format(this.nanText, value)); return false; } if(num < this.minValue){ this.markInvalid(String.format(this.minText, this.minValue)); return false; } if(num > this.maxValue){ this.markInvalid(String.format(this.maxText, this.maxValue)); return false; } return true; }, getValue : function(){ return this.fixPrecision(this.parseValue(Roo.form.NumberField.superclass.getValue.call(this))); }, // private parseValue : function(value){ value = parseFloat(String(value).replace(this.decimalSeparator, ".").split(this.thousandSeparator).join('')); return isNaN(value) ? '' : value; }, // private fixPrecision : function(value){ var nan = isNaN(value); if(!this.allowDecimals || this.decimalPrecision == -1 || nan || !value){ return nan ? '' : value; } return parseFloat(value).toFixed(this.decimalPrecision); }, setValue : function(v){ v = this.fixPrecision(v); if(this.thousandSeparator != ''){ v = Roo.util.Format.number(v, this.decimalPrecision, this.thousandSeparator); } Roo.form.NumberField.superclass.setValue.call(this, String(v).replace(".", this.decimalSeparator)); if (this.hiddenField !== false) { this.hiddenField.value = v ? this.parseValue(v) : ''; } }, // private decimalPrecisionFcn : function(v){ return Math.floor(v); }, beforeBlur : function(){ var v = this.parseValue(this.getRawValue()); if(v){ this.setValue(v); } } });