简体   繁体   中英

RegExp TAB in Firefox

This code works in all popular browsers except Mozilla Firefox. The problem is that the TAB-key doesn't work. Can anyone figure out why? It's connected to a form of text fields. I've tried adding '\\t', didn't work. It works in all browsers for me, except Firefox...

$('.mail').bind('keypress', function (event) {
    var regex = new RegExp("^[a-zA-Z0-9@\S._\n\r\b-]+$");
    var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
    if (!regex.test(key)) {
        event.preventDefault();
        return false;
    }
});

without going into the regular expression, you can easily allow the tab keypress by comparing against its code:

$('.mail').bind('keydown', function (event) {
    var regex = new RegExp("^[a-zA-Z0-9@\S._\n\r\b-]+$");
    var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);

    if (!regex.test(key) && event.which != 9) {
        event.preventDefault();
        return false;
    }
});

If you run into more cases that should work but don't it might be good to revisit the regex but if that's the only case then this quick workaround should be enough.

Also note I'm using keydown instead of keypress (event.which throws different codes on keypress)

jsfiddle

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