zoukankan      html  css  js  c++  java
  • JQ 限制文本框 数字 小数 字母

    最近在项目中要控制文件框的输入,Google了一下,找到一些方法。并修改。以下代码经过验证,可以正常使用。

    // ----------------------------------------------------------------------
    // <summary>
    // 文本框只能输入数字(不包括小数),并屏蔽输入法和粘贴
    // </summary>
    // ---------------------------------------------------------------------- 
    $.fn.integer = function () {
        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("paste", function () {
            return false;
        });
        this.bind("keyup", function () {
            if (/(^0+)/.test(this.value)) {
                this.value = this.value.replace(/^0*/, '');
            }
        });
        this.bind("focus", function () {
            this.style.imeMode = 'disabled';
        });
        this.bind("blur", function () {
            var reg = /^d{1,}$/;
            if (this.value.slice(-1) == "") {
                this.value = "0";
            }
            if (reg.test(this.value) === false) {
                this.value = "0";
            }
        });
    };
    
    // ----------------------------------------------------------------------
    // <summary>
    // 文本框只能输入数字(包括小数),并屏蔽输入法和粘贴 
    // </summary>
    // ----------------------------------------------------------------------
    $.fn.number = function () {
        this.bind("keypress", function (e) {
            var code = (e.keyCode ? e.keyCode : e.which); //兼容火狐 IE 
            if (!$.browser.msie && (e.keyCode == 0x8)) { //火狐下不能使用退格键 
                return;
            }
            if (this.value.indexOf(".") == -1) {
                return (code >= 48 && code <= 57) || (code == 46);
            } else {
                return code >= 48 && code <= 57
            }
        });
        this.bind("paste", function () {
            return false;
        });
        this.bind("keyup", function () {
            if (this.value.slice(0, 1) == ".") {
                this.value = "";
            }
        });
        this.bind("focus", function () {
            this.style.imeMode = 'disabled';
        });
        this.bind("blur", function () {
            var reg = /^-?[^D]+.?[^D]{1,4}$/;
            if (this.value.slice(-1) == ".") {
                this.value = this.value.slice(0, this.value.length - 1);
            }
            if (this.value.slice(-1) == "") {
                this.value = "0";
            }
            if (reg.test(this.value) === false) {
                this.value = "0";
            }
        });
    };
    
    // ----------------------------------------------------------------------
    // <summary>
    // 限制只能输入字母
    // </summary>
    // ----------------------------------------------------------------------
    $.fn.onlyAlpha = function () {
        this.bind("keypress", function (event) {
            var eventObj = event || e;
            var keyCode = eventObj.keyCode || eventObj.which;
            if ((keyCode >= 65 && keyCode <= 90) || (keyCode >= 97 && keyCode <= 122))
                return true;
            else
                return false;
        });
        this.bind("paste", function () {
            var clipboard = window.clipboardData.getData("Text");
            if (/^[a-zA-Z]+$/.test(clipboard))
                return true;
            else
                return false;
        });
        this.bind("focus", function () {
            this.style.imeMode = 'disabled';
        });
    };
    
    // ----------------------------------------------------------------------
    // <summary>
    // 限制只能输入数字和字母
    // </summary>
    // ----------------------------------------------------------------------
    $.fn.onlyNumAlpha = function () {
        this.bind("keypress", function (event) {
            var eventObj = event || e;
            var keyCode = eventObj.keyCode || eventObj.which;
            if ((keyCode >= 48 && keyCode <= 57) || (keyCode >= 65 && keyCode <= 90) || (keyCode >= 97 && keyCode <= 122))
                return true;
            else
                return false;
        });
        this.bind("paste", function () {
            var clipboard = window.clipboardData.getData("Text");
            if (/^(d|[a-zA-Z])+$/.test(clipboard))
                return true;
            else
                return false;
        });
        this.bind("focus", function () {
            this.style.imeMode = 'disabled';
        });
    };

    使用方法以:

    $("#Id").integer();
    $("#Id").number();
    $("#Id").onlyAlpha();
    $("#Id").onlyNumAlpha();
  • 相关阅读:
    操作系统面试题(四)
    计算机网络面试题(三)
    redis和mongodb面试题(一)
    MySQL面试题(二)
    数据库基础面试题(一)
    RoBERTa模型总结
    BERT模型总结
    动态规划-Dynamic Programming(DP)
    XGBoost原理学习总结
    re模块的使用
  • 原文地址:https://www.cnblogs.com/Haibocai/p/4741808.html
Copyright © 2011-2022 走看看