我想在一个列表中允许某些字符,而在另一列表中阻止其他字符。

允许这些:

[A-Za-z0-9 ,.)(]


预防这些:

[^~!@#$%^&*_+]


这是失败的:
为什么会失败?

(function($) {
    $.fn.extend({
        standardOnly: function() {
            return this.each(function() {
                return $(this).keypress(function(e, text) {

                    var keynum;
                    var keychar;
                    var regEx;
                    var allowedKeyNums = [8, 9, 35, 36, 46]; // Backspace, Tab, End, Home, (Delete & period)

                    if (window.event) // IE
                        keynum = e.keyCode;
                    else if (e.which) // Netscape/Firefox/Opera
                        keynum = e.which;
                    else
                        keynum = e.keyCode

                    keychar = String.fromCharCode(keynum);
                    regEx = /[^#$]/ // Undesirable characters

                    // Test for keynum values that collide with undesirable characters
                    if ($.inArray(keynum, allowedKeyNums) > -1)
                        return regEx.test(keychar);

                    regEx = /[A-Za-z0-9 ,.)(][^~!@#$%^&*_+]/
                    return regEx.test(keychar);
                });
            });
        }
    });
})(jQuery);

最佳答案

您要测试的是一个有效字符,后跟一个无效字符。只寻找允许的字符。如果失败,就完成了,对不对?

09-25 18:37