JavaScript keypress issues

The following is simply trying to filter the characters the user can input. There are two issues:

1) For some reason, when I try to get the val() of the text field through jQuery, the value I'm getting back is one character behind. For example, if I type in 'a', the value I get back is an empty string. When I type a 'b' so that the value should now be 'ab', I get only 'a', and so on.

2) When I press backspace, the keypress event is not firing. It's important that I be able to detect that, too.

Can anyone tell me why this is behaving this way? And what can I do rectify it? Thanks.

<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
    $(function() {
        var sAllowableCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890-',.";
        $("#txtText").keypress(function(event) {
            var s = String.fromCharCode(event.keyCode);
            if (sAllowableCharacters.indexOf(s) === -1) {
                return false;
            }
            console.log($("#txtText").val());
        });
    });

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

Open in new window

elepilAsked:
Who is Participating?
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.

RobOwner (Aidellio)Commented:
Yep, because it hasn't been inserted yet.  Do you need to get the value right at that point?  otherwise just have keyup event to capture what the value is
0
RobOwner (Aidellio)Commented:
i.e.
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
    $(function() {
        var sAllowableCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890-',.";
        $("#txtText").keypress(function(event) {
            var s = String.fromCharCode(event.keyCode);
            if (sAllowableCharacters.indexOf(s) === -1) {
                return false;
            }
        });
        $("#txtText").keyup(function(event) {
            console.log($("#txtText").val());
        });
    });

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

Open in new window

0

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:
For some reason, I always thought I could handle all in just one type of keyboard event. I guess I really need to use multiple events to make this thing work as I want. Thanks for all your help, Rob!
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.