Disable some characters from input field

with JQuery,

$("input").keypress( function(e) {
    var chr = String.fromCharCode(e.which);
    if ("12345NOABC".indexOf(chr) < 0)
        return false;
});

without JQuery

document.getElementById("foo").onkeypress = function(e) {
    var chr = String.fromCharCode(e.which);
    if ("12345NOABC".indexOf(chr) < 0)
        return false;
};

For one liners, from @mplungjan and @matthew-lock’s comment

document.querySelector("#foo").onkeypress = function(e) {
    return "12345NOABC".indexOf(String.fromCharCode(e.which)) >= 0;
};

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)