我想在一个列表中允许一些字符,并阻止其他列表中的其他字符。
允许这些:
[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);
答案 0 :(得分:4)
您正在测试的是有效字符,后跟无效字符。只是寻找允许的角色。如果它失败了,你就完蛋了吧?