Javascript, how to prevent invalid characters from showing in a text field

Let's say I only want the user to input alphabetic and numeric (no punctuations of any kind) within a text field. I can detect this through the various key events, but what I really want to accomplish is to prevent the invalid character from even showing in the text field.

For example, if I keyed in A, it would show in the text field since it's valid. But when I type in a '>', it is invalid and i don't want it to show in the text field. I've tried using event.preventDefault(), but it's not working.

Suggestions, anyone? Here is my code snippet:

<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
    $(function() {
        $("#txtText").keyup(function(event) {
            var sInput = String.fromCharCode(event.keyCode);
            if ("ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".indexOf(sInput) !== -1) {
                console.log(sInput);
            } else {
                console.log("Invalid character");
                event.preventDefault(); // NOT WORKING, CHARACTER STILL SHOWS IN THE TEXT FIELD.
            }
        });
    });

</script>
<style>
</style>
</head>
<body>    
    <input id="txtText" />
</body>
</html>

Open in new window

elepilAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Russ SuterSenior Software DeveloperCommented:
You could try something like this:

$(document).ready(function() {
    $('#contactNumber').keyup(function() {
        var numbers = $(this).val();
        $(this).val(numbers.replace(/\D/, ''));
    });
});

Open in new window


Or, instead of event.PreventDefault() you could just return false. That should do the trick.
RobOwner (Aidellio)Commented:
Problem is the event you're using.  Keyup has already inserted the character.  Use Keydown instead.

Demo: http://jsbin.com/mapero/1/edit

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
elepilAuthor Commented:
You nailed it, Rob. Here's a part 2 to my question at http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28681719.html.

Thanks to Russ, too, for responding.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.