0

I am using JavaScript keycodes, they are not working in Firefox but working in Chrome and IE. I have debugged the code in front end for Firefox I am getting keycode as 0.

This is my code:

$scope.Validate = function(event,indexVal){
    if ((event.keyCode > 64 && event.keyCode < 91)|| (event.keyCode > 159 && event.keyCode < 166) || (event.keyCode > 96 && event.keyCode < 123) || (event.keyCode == 165) ||(event.keyCode == 32)
                        || (event.keyCode == 164) || (event.keyCode == 130) || (event.keyCode == 181) || (event.keyCode == 144) || (event.keyCode == 214) ||
                        (event.keyCode == 224) ||(event.keyCode == 233)) {
                }else{
                    event.preventDefault();
                }
}

Can you please suggest a way to achieve this functionality in Firefox too.

Meraj Ahmed
  • 5
  • 1
  • 3
Murali
  • 358
  • 5
  • 15

2 Answers2

0

It's working but slightly different

$scope.Validate = function (event, indexVal) {
    var code = event.which || event.keyCode;

    if (!(
            (code > 64 && code < 91)
            || (code > 159 && code < 166) 
            || (code > 96 && code < 123) 
            || ~[165, 32, 164, 130, 181, 144, 214, 224, 233].indexOf(code)
    )) {
        event.preventDefault();
    }
}

https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode

Deprecated KeyboardEvent.keyCode This feature has been removed from the Web standards. Though some browsers may still support it, it is in the process of being dropped. Avoid using it and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

FieryCat
  • 1,875
  • 18
  • 28
  • nice let me try once – Murali Jun 09 '17 at 05:40
  • $scope.Validate = function(event,indexVal){} do I need to pass this event and indexVal to this function – Murali Jun 09 '17 at 05:40
  • `event` will be enough; but, possibly for other validation purposes `indexVal` will be used as well – FieryCat Jun 09 '17 at 05:43
  • 1
    Just FYI: `KeyboardEvent.which` is also marked at MDN as [deprecated](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/which) – barbsan Jun 09 '17 at 05:43
  • @Murali, it won't have some differences at all... but I've updated the answer. As I've mentioned, if `indexVal` is not in use, just remove it from scope – FieryCat Jun 09 '17 at 05:49
0

In Firefox, event.keyCode does not always work, depending on the binding event. You'll have to use event.which. Refer to this post for more info.

$scope.Validate = function(event,indexVal) {
  var key = event.keyCode || event.which;
  if ((key > 64 && key < 91) || 
    (key > 159 && key < 166) || 
    (key > 96 && key < 123) || 
    (key == 165) ||
    (key == 32) ||
    (key == 37) ||
    (key == 39) ||
    (key == 164) || 
    (key == 130) || 
    (key == 181) || 
    (key == 144) || 
    (key == 214) ||
    (key == 224) ||
    (key == 233)
  ) {
    // Do something.
  } else {
    event.preventDefault();
  }
}
Yangshun Tay
  • 49,270
  • 33
  • 114
  • 141
  • 1
    working fine but how can I allow left and right arrow in mozilla – Murali Jun 09 '17 at 07:29
  • Arrow keys have the keycode from 37 to 40 (left, up, right, down) respectively. So you'll have to add them into your conditions. – Yangshun Tay Jun 09 '17 at 07:37
  • in chrome it is accepting by default. but even if I keep 37 it is not allowing arrow. – Murali Jun 09 '17 at 07:51
  • it is working like http://www.theasciicode.com.ar/ascii-printable-characters/capital-letter-a-uppercase-ascii-code-65.html – Murali Jun 09 '17 at 07:51
  • ASCII is not keycode! Keycodes are number representations of the keys on your keyboard, whereas ASCII is the number representation of a particular character. – Yangshun Tay Jun 09 '17 at 07:58
  • can you please give me some example for writing ascii code of left and right arrow – Murali Jun 09 '17 at 09:42
  • It's not ASCII code you need... Anyway, add two conditions to check for 37 and 39 in the if clause. Updated my answer for it – Yangshun Tay Jun 09 '17 at 09:44