3

I am trying to build a input field which will only take number as input so it's input type is number. But the keyboard visible on Android device is having a "." button on keyboard, so I am trying to prevent that button. using below code

onKeyDown = function(event){
          console.log('KEY: ' + event.which);
          if (event.which == 250 || event.which == 229) {
                event.preventDefault();
          }
      };

229 is the ASCII code for the "." key of numbering keyboard in Android. Please help me

Updated: 1) This should work on mobile device. 2) Input type is "number" All solutions work on browser but not in mobile.

Thank You Sandip Jadhav

Sandip Jadhav
  • 7,377
  • 8
  • 44
  • 76
  • http://stackoverflow.com/questions/25413145/restricting-characters-in-input-field-to-a-set-of-characters check this..this might help – varun aaruru May 03 '17 at 11:39

2 Answers2

0

I am not sure but you could try different approach

I suppose $scope.input its a ngModel that contains input value

onKeyDown = function(event){
      console.log('KEY: ' + event.which);
      if (event.which == 250 || event.which == 229) {
            $scope.input = $scope.input.substring(0, $scope.input.length - 1);
      }
  };

Warning it will work only if cursor at the end of the string

Also check this topic Restricting Characters in Input Field to a Set of Characters

Community
  • 1
  • 1
Leguest
  • 2,097
  • 2
  • 17
  • 20
0

its better to use preventDefault as inline

<input name="firstname" type="text" ng-model="contact.first_name" required="required" placeholder="first name" ng-keypress="($event.which==32 || $event.which==229)?$event.preventDefault():''">

it will check for both keys and prevent inserting it impotent to note it wont prevent from pasting these codes.here i have added keycode 32 for testing purpose it prevent inserting spaceba,change it as your requirement for more see this pen http://codepen.io/edisonpappi/pen/YVxLxX?editors=1010

Edison Augusthy
  • 1,535
  • 17
  • 32