文本框只能输入金额,并屏蔽输入法和粘贴

/**
* 文本框只能输入数字,并屏蔽输入法和粘贴  
*/

$.fn.numeral = function () {
    $(this).css("ime-mode", "disabled");
    this.bind("keypress", function (e) {
        var code = (e.keyCode ? e.keyCode : e.which);  //兼容火狐 IE     
        if (!$.browser.msie && (e.keyCode == 0x8))  //火狐下不能使用退格键    
        {
            return;
        }
        return code >= 48 && code <= 57;
    });
    this.bind("blur", function () {
        if (this.value.lastIndexOf(".") == (this.value.length - 1)) {
            this.value = this.value.substr(0, this.value.length - 1);
        } else if (isNaN(this.value)) {
            this.value = "";
        } else {
            this.value = this.value.replace(/[^\d]+?/g, '')
        }
    });
    this.bind("paste", function () {
        var s = clipboardData.getData('text');
        if (!/\D/.test(s));
        value = s.replace(/^0*/, '');
        return false;
    });
    this.bind("dragenter", function () {
        return false;
    });
    this.bind("keyup", function () {
        if (/(^0+)/.test(this.value)) {
            this.value = this.value.replace(/^0*/, '');
        }
    });
};
/**
* 文本框只能输入金额,并屏蔽输入法和粘贴  
*/

$.fn.money = function () {
    $(this).css("ime-mode", "disabled");
    this.bind("keypress", function (e) {
        var code = (e.keyCode ? e.keyCode : e.which);  //兼容火狐 IE     
        if (!$.browser.msie && (e.keyCode == 0x8))  //火狐下不能使用退格键    
        {
            return;
        }
        if (code == 46) {
            return true;
        }

        return code >= 48 && code <= 57;
    });
    this.bind("blur", function () {
        if (isNaN(this.value)) {
            this.value = "";
        }
        else {
            this.value = this.value.replace(/[^\d\.]+?/g, '')
        }
    });
    this.bind("paste", function () {
        var s = clipboardData.getData('text');
        if (!/\D\./.test(s));
        value = s.replace(/^0*/, '');
        return false;
    });
    this.bind("dragenter", function () {
        return false;
    });
    this.bind("keyup", function () {
        if (/(^0+)/.test(this.value)) {
            this.value = this.value.replace(/^0*/, '');
        }
    });
};  

//页面加载后公用
$(function () {
    $("input[type=text]").focus(function () {
        $(this).removeClass("textBlur");
        $(this).addClass("textFocus");
    }).blur(function () {
        $(this).removeClass("textFocus");
        $(this).addClass("textBlur");
    });
    $(".textnumber").numeral();
    $(".textmoney").money();

});

你可能感兴趣的:(js方法)