Keycode Tester

by markallgood

HTML

Key Entry:
<input id="kctest" type="text" value="test value" />

JavaScript

$(function () {
    $("#kctest").keypress(function (e) {
        alert("keyPress keyCode:" + e.keyCode + "\ncharCode:" + e.charCode + "\nstring:" + String.fromCharCode(e.charCode));

        var allow = false;

        // Allow alpha?
        if (false) {
            allow = (allow || /^[a-zA-Z]+$/.test(String.fromCharCode(!event.charCode ? event.which : event.charCode)));
        }

        // Allow numeric?
        if (true) {
            allow = (allow || /^[0-9]+$/.test(String.fromCharCode(!event.charCode ? event.which : event.charCode)));
        }

        // Allow decimals?
        if (false) {
            allow = (allow || /^[.]+$/.test(String.fromCharCode(!event.charCode ? event.which : event.charCode)));
        }

        // Allow dashes?
        if (false) {
            allow = (allow || /^[-]+$/.test(String.fromCharCode(!event.charCode ? event.which : event.charCode)));
        }

        // Allow space?
        if (false) {
            allow = (allow || String.fromCharCode(!event.charCode ? event.which : event.charCode) == " ");
        }

        // Allow forward slashes?
        if (true) {
            allow = (allow || /\//.test(String.fromCharCode(!event.charCode ? event.which : event.charCode)));
        }

        if (!allow) {
            event.preventDefault();
        }
    });
    //  $("#kctest").keydown(function(e) {
    //      if (e.keyCode != 16) // ignore tabs
    //          alert("keydown keyCode:" + e.keyCode);
    //  });
});