简体   繁体   中英

jQuery KeyFilter Plugin Problem

Using keyfilter plugin for jQuery, and all seems to be fine apart from one problem.

I am using a regular expression to filter the element.

$('#nameVal').keyfilter(/[0-9a-zA-Z]/);

The odd thing is, this is not only allowing alpha-numeric characters but it is also allowing '(' to be entered. In fact, it doesn't matter what expression I pass to the keyfilter, I can't stop '(' being allowed in the textbox.

Has anyone else experienced this problem and is there a solution?

I found the problem.

The isSpecialKey function in the KeyFilter plugin returns true if the keycode is 40, which is the keycode for '('. This means that the test is never performed on the character.

var isSpecialKey = function(e)
{
    var k = e.keyCode;
    var c = e.charCode;
    return k == 9 || k == 13 || /*(k == 40 && (!$.browser.opera || !e.shiftKey)) ||*/ k == 27 ||
        k == 16 || k == 17 ||
        (k >= 18 && k <= 20) ||
        ($.browser.opera && !e.shiftKey && (k == 8 || (k >= 33 && k <= 35) || (k >= 36 && k <= 39) || (k >= 44 && k <= 45)))
        ;

    };

Commenting out the bit of code as I have above fixes the problem. Not entirely sure what effect this will have in other browsers, but it works in IE8 and Chrome.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM