gaurav gaurav - 2 months ago 6
Javascript Question

jQuery select input type text where ID not equal to

I wrote the code listed below in order to restrict user input on all inputs whose type is

text
. Now I want to exclude all inputs whose ID is
allowall
from this restriction. How can I modify the code to allow this?

$('input[type=text]').keypress(function (e) {
var regex = new RegExp("^[a-zA-Z0-9 ]+$");
var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
if (regex.test(str)) {
return true;
}
e.preventDefault();
return false;
});

Answer
$('input[type=text]').not('#allowall').keypress(function (e) {
    var regex = new RegExp("^[a-zA-Z0-9 ]+$");
    var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
    if (regex.test(str)) {
        return true;
    }
    e.preventDefault();
    return false;
});
Comments