0

I'm seeing a wierd issue in javascript, or maybe it's because javascript is new to me.

I am trying to allow some action if onkeypress, but only when alphanumeric or special character is pressed. This is my code:

if(e.keyCode>=32 && e.keyCode<=126 ){
    alert(e.keyCode);}
}

It does not alert if I press any special character key like {,]>. am I missing anything here?

4
  • 3
    Could you post your entire code? The above works fine for me: jsfiddle.net/LZhSW Commented Jan 28, 2013 at 13:13
  • Does it work when you press a numerical key (1, 2, 3)? Commented Jan 28, 2013 at 13:16
  • yes, it does work for numerical key as well. I took the ASCII code from W3 w3schools.com/tags/ref_ascii.asp , but when i press any special character like [{]{><,.; it does not fall in =>32 and <=126 Commented Jan 28, 2013 at 13:27
  • Which browser are you using? Commented Jan 28, 2013 at 13:42

4 Answers 4

1

The keyCode for a comma (,) is 188. Could this be why?

http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes

Sign up to request clarification or add additional context in comments.

6 Comments

The key being that keyCode is not a reference into the ASCII table
I took referenced from W3schools url w3schools.com/tags/ref_ascii.asp and all special char comes under 32 to 126.. Am I refering wrong website to get ASCII code?
@user2018223: You shouldn't be looking for ASCII codes at all, because you're using keyCode.
I am new in this field , please let me know how does it differ keyCode from ASCII codes?
@user2018223: Didn't you read my answer? It contains tons of links explaining this. There is also unixpapa.com/js/key.html. Please read.
|
1

You have assumed that keyCode gives you an ASCII index, but that is not true. Your range criteria are incorrect due to this assumption.

MDN says:

In a keypress event, the Unicode value of the key pressed is stored in either the keyCode or charCode property, never both. If the key pressed generates a character (e.g. 'a'), charCode is set to the code of that character, respecting the letter case. (i.e. charCode takes into account whether the shift key is held down). Otherwise, the code of the pressed key is stored in keyCode.

keyCode is always set in the keydown and keyup events. In these cases, charCode is never set.

To get the code of the key regardless of whether it was stored in keyCode or charCode, query the which property.

For a list of the keyCode values associated with particular keys, run the example in Example 7: Displaying Event Object Constants and view the resulting HTML table.

Doing as instructed reveals that some of your "special" characters lie outside of the range limits you've specified.

Kevin also pointed out this useful table, but you'd be best off using a more canonical approach to keypress handling.

Comments

1

You need to use the which property in most browsers and keyCode in IE only. You do have the correct event: only keypress will do if you're interested in the character typed rather than the physical key pressed.

Here's my favourite reference for this stuff: http://unixpapa.com/js/key.html

document.onkeypress = function(e) {
    e = e || window.event;
    var charCode = (typeof e.which == "number") ? e.which : e.keyCode;
    var charTyped = String.fromCharCode(charCode);

    alert("Character typed: " + charTyped);
};

3 Comments

Ah, excellent - I didn't fancy converting your previous answer. :)
@Non-StopTimeTravel: I seem to churn out variations on this answer on a weekly basis :)
Sounds like it's time for a pseudo-canonical self-answered FAQ post on the topic!
0

You should use which, it should work in both Firefox and Chrome.

Try this:

document.onkeypress = function(e) {
    console.log(e.keyCode + " -- " + e.which + " -- " + e.charCode);

    if(e.which >= 32 && e.which <= 126 ){
        alert(e.which);
    }
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.