Sebastian Sebald
Sebastian Sebald

Reputation: 16856

Check if keydown event for umlaute

I want to check via eventListener if the pressed key is an umlaut (ä, ö, ü). The problem is that the charCode/keyCode is always 0 and if I am not mistaken the value is also used for some control keys.

I tried tried to parse the e.which of the keyDown to something I can work with, but until now nothing seems to work. It would be nice if I could do something like 'ä'.atCharCode().

Any help? :) Or is it fine and safe to check for the 0?

Upvotes: 1

Views: 2170

Answers (3)

Scribilicious
Scribilicious

Reputation: 379

Firefox uses for these particular characters the key-code 0. For all other browser it is 186, 192 and 222.

For example if you want to check if alphabetical keys + umlauts are pressed I use the following code:

$('.myInputBox').keydown(function(e) {
    if ($.inArray(e.keyCode, [0, 186, 192, 222]) != -1 || (e.keyCode > 64 && e.keyCode < 91)) {  
        alert('valid key!');
    } else {
        e.preventDefault();
        alert('no valid key!');
    }
});

Upvotes: 2

Sebastian Sebald
Sebastian Sebald

Reputation: 16856

Found the problem. In Chrome everthing is fine and umlaute are getting the correct charCode. In Firefox on the other hand it seems like there is no real support for umlaute and their charCode is always 0. The FF I tested in was 17.0.1

So if you want to solve this problem, you have to don't allow events with a e.keyCode/e.which of 0.

Upvotes: 1

sreejithsdev
sreejithsdev

Reputation: 1210

<script type="text/javascript">
        function disablekeys(e) {
            var e = event || evt;
            var charCode = e.which || e.keyCode;
            if (charCode = 8) {
                //your code here
            }
        }
</script>

Find char code from following link http://www.expandinghead.net/keycode.html

Upvotes: 0

Related Questions