0

我有 2 个要应用遮罩的文本区域。

  1. 文本区域 1:多个 5 位邮政编码,以逗号和空格分隔
  2. 文本区域 2:多个 3 位邮政编码,以逗号和空格分隔

所以在这两种情况下,允许的字符都是 0-9 以及逗号和空格。

我很难为此想出一个掩蔽。我可以用蒙面插件做这样的事情吗?

http://digitalbush.com/projects/masked-input-plugin/

我按照这个提出了一个自定义插件来允许特定的键,但遇到了逗号和 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;
        });
    });
};
4

2 回答 2

1

这是我用来屏蔽数字输入的代码

$('.numeric').keyup(function() {
    $(this).val($(this).val().replace(/[^0-9.]/g,''))
}); 

因此,更改正则表达式应该可以帮助您实现您想要做的事情。我不是正则表达式专家,但有很多在线资源!

通过将“数字”类添加到要屏蔽的输入字段来使用它 - 显然将类名更改为更合适的名称!

于 2013-09-27T09:42:49.067 回答
0

这里是插件...

//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');
    });

};
于 2013-09-27T14:43:04.483 回答