Javascript 用于邮政编码的jquery屏蔽插件

Javascript 用于邮政编码的jquery屏蔽插件,javascript,jquery,Javascript,Jquery,我有两个文本区域,我想应用遮罩 文本区域1:由逗号和空格分隔的多个5位邮政编码 文本区域2:由逗号和空格分隔的多个3位邮政编码 因此,在这两种情况下,允许的字符都是0-9、逗号和空格 我很难想出一个解决这个问题的办法。我可以用屏蔽插件做类似的事情吗 我随后提出了一个自定义插件,允许使用特定的键,但遇到了逗号和ctrl+V的问题。逗号和=48&&key=96&&key以下是我用来屏蔽输入的代码,仅用于数字 $('.numeric').keyup(function() { $(this).

我有两个文本区域,我想应用遮罩

  • 文本区域1:由逗号和空格分隔的多个5位邮政编码
  • 文本区域2:由逗号和空格分隔的多个3位邮政编码
  • 因此,在这两种情况下,允许的字符都是0-9、逗号和空格

    我很难想出一个解决这个问题的办法。我可以用屏蔽插件做类似的事情吗

    我随后提出了一个自定义插件,允许使用特定的键,但遇到了逗号和ctrl+V的问题。逗号和<都有相同的键代码,所以现在采取屏蔽路线

    //Multiple zip codes separated by comma and space
    jQuery.fn.multipleZipCodesSeparatedByCommaAndSpaceOnly = function () {
        return this.each(function () {
            $(this).keydown(function (e) {
                var key = e.which || e.keyCode;
                //alert(String.fromCharCode(key));
                if (!e.altKey && e.ctrlKey && //&&  !e.shiftKey && 
                // numbers   
                    (key >= 48 && key <= 57) ||
                // Numeric keypad
                    (key >= 96 && key <= 105) ||
                // comma, space
                    key == 188 || key == 32 ||
                // Backspace and Tab
                    key == 8 || key == 9 ||
                // Home and End
                    key == 35 || key == 36 ||
                // left and right arrows
                    key == 37 || key == 39 ||
                // Del and Ins
                    key == 46 || key == 45) {
                    return true;
                }
    
                return false;
            });
        });
    };
    
    //由逗号和空格分隔的多个邮政编码
    jQuery.fn.multiplezipcodeseparatedbycommaandspaceonly=函数(){
    返回此。每个(函数(){
    $(此).keydown(函数(e){
    var key=e.which | e.keyCode;
    //警报(String.fromCharCode(键));
    如果(!e.altKey&&e.ctrlKey&&/&&&e.shiftKey&&
    //数字
    
    (key>=48&&key=96&&key以下是我用来屏蔽输入的代码,仅用于数字

    $('.numeric').keyup(function() {
        $(this).val($(this).val().replace(/[^0-9.]/g,''))
    }); 
    
    因此,更改正则表达式应该有助于实现您想要做的事情。 我不是regex专家,但有很多在线资源

    通过在你想要屏蔽的输入字段中添加一个“numeric”类来使用它-显然,将类名更改为更合适的名称!

    这是插件

    //Multiple zip codes separated by comma and space
    jQuery.fn.multipleZipCodesSeparatedByCommaAndSpaceOnly = function (method) {
    
        var methods = {
            init: function () {
                //keyup
                $(this).bind('input propertychange', function (e) {
                    methods.ZipCodeHandle(e, $(this));
                });
            },
            ZipCodeHandle: function (e, $object) {
                $object.val($object.val().replace(/[^\d\, ]/g, ''));
            }
        };
    
        return this.each(function () {
            // Method calling logic
            if (methods[method])
                return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
            else if (typeof method === 'object' || !method)
                return methods.init.apply(this, arguments);
            else
                $.error('Method ' + method + ' does not exist');
        });
    
    };
    

    谢谢,我把我的实现放在下面,这样我可以正确格式化它。