0
我想允許一個列表中的某些字符,並阻止其他列表中的其他列表。允許並防止正則表達式中的某些字符?
讓這些:
[A-Za-z0-9 ,.)(]
防止這些:
[^[email protected]#$%^&*_+]
這是失敗的:
爲什麼會失敗?
(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 ,.)(][^[email protected]#$%^&*_+]/
return regEx.test(keychar);
});
});
}
});
})(jQuery);
上帝......我是個笨蛋 –
只有當他錨定表達式'/^[允許的字符] + $ /'。 –
@Felix:在這種情況下按鍵不一定總是被稱爲字符。 –