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验证的时候,无法通过,总是先试试不是有效值,这个就没有问题了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值