ext numberfield添加金额输入效果

        if (Ext.form.NumberField) {
            Ext.apply(
								Ext.form.NumberField.prototype,
								{
								    decimalPrecision: 2,
								    // allowNegative :false,
								    allowDecimals: true
								    // allowBlank : false
									,
								    cls: 'numReadOnly'
								    // private
									,
								    FormatComma: true
								    // private
									,
								    initEvents: function () {
								        Ext.form.NumberField.superclass.initEvents
												.call(this);
								        var allowed = this.baseChars + '';
								        if (this.allowDecimals) {
								            allowed += this.decimalSeparator;
								        }
								        if (this.FormatComma) {
								            allowed += ",";
								        }
								        if (this.allowNegative) {
								            allowed += "-";
								        }
								        this.stripCharsRe = new RegExp(
												'[^' + allowed + ']', 'gi');
								        var keyPress = function (e) {
								            var k = e.getKey();
								            if (!Ext.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) {
							        var tmpvalue = this.removeCommas(String(value));
								        if (!Ext.form.NumberField.superclass.validateValue
												.call(this, tmpvalue)) {
								            return false;
								        }
//								        if (!Ext.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;
								        }
								        if (this.FormatComma) {
								            value = this.removeCommas(String(value));
								        }
								        value = String(value).replace(
												this.decimalSeparator, ".");
								        if (isNaN(value)) {
								            this.markInvalid(String.format(
													this.nanText, value));
								            return false;
								           
								        }
								        var num = this.parseValue(value);
								        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;
								    },
								    fixPrecision: function (value) {
								        var nan = isNaN(value);
								        if (!this.allowDecimals
												|| this.decimalPrecision == -1
												|| nan || !value) {
								            return nan ? '' : value;
								        }
								        return parseFloat(parseFloat(value)
												.toFixed(this.decimalPrecision));
								    },

								    setValue: function (v) {
								        v = typeof v == 'number' ? v : (String(
												this.removeCommas(v)).replace(
												this.decimalSeparator, ".")
												);
								        v = isNaN(v) ? '' : String(v).replace(
												".", this.decimalSeparator);
								        if (String(v).length > 0)
								            v = parseFloat(v).toFixed(
													this.decimalPrecision);
								        // if(this.FormatComma)
								        // v=this.formatCommaStyle(v);
								        Ext.form.NumberField.superclass.setValue
												.call(this, v);
								        if (this.FormatComma
												&& String(v).length > 0) {
								            v = this.addCommas(v);
								            Ext.form.NumberField.superclass.setRawValue
													.call(this, v);
								        }
								    },
								    parseValue: function (value) {
								        if (this.FormatComma)
								            value = this.removeCommas(String(value));
								        value = parseFloat(String(value)
												.replace(this.decimalSeparator,
														"."));

								        return isNaN(value) ? '' : value;
								    },
								    beforeBlur: function () {
								        var v = this.parseValue(this
												.getRawValue());

								        if (String(v).trim().length > 0) {
								            this.setValue(this.fixPrecision(v));

								        }
								    }

									,
								    addCommas: function (nStr) {

								        nStr += '';
								        if (nStr.length == 0)
								            return '';
								        x = nStr.split('.');
								        x1 = x[0];
								        x2 = x.length > 1 ? '.' + x[1] : '';
								        var rgx = /(\d+)(\d{3})/;
								        while (rgx.test(x1)) {
								            x1 = x1.replace(rgx,
													'$1' + ',' + '$2');
								        }
								        return x1 + x2;

								    },
								    removeCommas: function (nStr) {

								        nStr = nStr + '';
								        var r = /(\,)/;
								        while (r.test(nStr)) {
								            nStr = nStr.replace(r, '');
								        }
								        return nStr;

								    }

								});

        }


这个是网上搜到了,稍微修改了下,原先的有个问题,就是在进行form验证的时候,无法通过,总是先试试不是有效值,这个就没有问题了

你可能感兴趣的:(Ext.Net)