+++ /dev/null
-/*\r
- * Ext JS Library 2.2.1\r
- * Copyright(c) 2006-2009, Ext JS, LLC.\r
- * licensing@extjs.com\r
- * \r
- * http://extjs.com/license\r
- */\r
-\r
-/**\r
- * @class Ext.form.NumberField\r
- * @extends Ext.form.TextField\r
- * Numeric text field that provides automatic keystroke filtering and numeric validation.\r
- * @constructor\r
- * Creates a new NumberField\r
- * @param {Object} config Configuration options\r
- */\r
-Ext.form.NumberField = Ext.extend(Ext.form.TextField, {\r
- /**\r
- * @cfg {RegExp} stripCharsRe @hide\r
- */\r
- /**\r
- * @cfg {String} fieldClass The default CSS class for the field (defaults to "x-form-field x-form-num-field")\r
- */\r
- fieldClass: "x-form-field x-form-num-field",\r
- /**\r
- * @cfg {Boolean} allowDecimals False to disallow decimal values (defaults to true)\r
- */\r
- allowDecimals : true,\r
- /**\r
- * @cfg {String} decimalSeparator Character(s) to allow as the decimal separator (defaults to '.')\r
- */\r
- decimalSeparator : ".",\r
- /**\r
- * @cfg {Number} decimalPrecision The maximum precision to display after the decimal separator (defaults to 2)\r
- */\r
- decimalPrecision : 2,\r
- /**\r
- * @cfg {Boolean} allowNegative False to prevent entering a negative sign (defaults to true)\r
- */\r
- allowNegative : true,\r
- /**\r
- * @cfg {Number} minValue The minimum allowed value (defaults to Number.NEGATIVE_INFINITY)\r
- */\r
- minValue : Number.NEGATIVE_INFINITY,\r
- /**\r
- * @cfg {Number} maxValue The maximum allowed value (defaults to Number.MAX_VALUE)\r
- */\r
- maxValue : Number.MAX_VALUE,\r
- /**\r
- * @cfg {String} minText Error text to display if the minimum value validation fails (defaults to "The minimum value for this field is {minValue}")\r
- */\r
- minText : "The minimum value for this field is {0}",\r
- /**\r
- * @cfg {String} maxText Error text to display if the maximum value validation fails (defaults to "The maximum value for this field is {maxValue}")\r
- */\r
- maxText : "The maximum value for this field is {0}",\r
- /**\r
- * @cfg {String} nanText Error text to display if the value is not a valid number. For example, this can happen\r
- * if a valid character like '.' or '-' is left in the field with no number (defaults to "{value} is not a valid number")\r
- */\r
- nanText : "{0} is not a valid number",\r
- /**\r
- * @cfg {String} baseChars The base set of characters to evaluate as valid numbers (defaults to '0123456789').\r
- */\r
- baseChars : "0123456789",\r
-\r
- // private\r
- initEvents : function(){\r
- Ext.form.NumberField.superclass.initEvents.call(this);\r
- var allowed = this.baseChars+'';\r
- if(this.allowDecimals){\r
- allowed += this.decimalSeparator;\r
- }\r
- if(this.allowNegative){\r
- allowed += "-";\r
- }\r
- this.stripCharsRe = new RegExp('[^'+allowed+']', 'gi');\r
- var keyPress = function(e){\r
- var k = e.getKey();\r
- if(!Ext.isIE && (e.isSpecialKey() || k == e.BACKSPACE || k == e.DELETE)){\r
- return;\r
- }\r
- var c = e.getCharCode();\r
- if(allowed.indexOf(String.fromCharCode(c)) === -1){\r
- e.stopEvent();\r
- }\r
- };\r
- this.el.on("keypress", keyPress, this);\r
- },\r
-\r
- // private\r
- validateValue : function(value){\r
- if(!Ext.form.NumberField.superclass.validateValue.call(this, value)){\r
- return false;\r
- }\r
- if(value.length < 1){ // if it's blank and textfield didn't flag it then it's valid\r
- return true;\r
- }\r
- value = String(value).replace(this.decimalSeparator, ".");\r
- if(isNaN(value)){\r
- this.markInvalid(String.format(this.nanText, value));\r
- return false;\r
- }\r
- var num = this.parseValue(value);\r
- if(num < this.minValue){\r
- this.markInvalid(String.format(this.minText, this.minValue));\r
- return false;\r
- }\r
- if(num > this.maxValue){\r
- this.markInvalid(String.format(this.maxText, this.maxValue));\r
- return false;\r
- }\r
- return true;\r
- },\r
-\r
- getValue : function(){\r
- return this.fixPrecision(this.parseValue(Ext.form.NumberField.superclass.getValue.call(this)));\r
- },\r
-\r
- setValue : function(v){\r
- v = typeof v == 'number' ? v : parseFloat(String(v).replace(this.decimalSeparator, "."));\r
- v = isNaN(v) ? '' : String(v).replace(".", this.decimalSeparator);\r
- Ext.form.NumberField.superclass.setValue.call(this, v);\r
- },\r
-\r
- // private\r
- parseValue : function(value){\r
- value = parseFloat(String(value).replace(this.decimalSeparator, "."));\r
- return isNaN(value) ? '' : value;\r
- },\r
-\r
- // private\r
- fixPrecision : function(value){\r
- var nan = isNaN(value);\r
- if(!this.allowDecimals || this.decimalPrecision == -1 || nan || !value){\r
- return nan ? '' : value;\r
- }\r
- return parseFloat(parseFloat(value).toFixed(this.decimalPrecision));\r
- },\r
-\r
- beforeBlur : function(){\r
- var v = this.parseValue(this.getRawValue());\r
- if(v || v === 0){\r
- this.setValue(this.fixPrecision(v));\r
- }\r
- }\r
-});\r
-Ext.reg('numberfield', Ext.form.NumberField);
\ No newline at end of file