-1

How can I call a function that fires only after an interger value is typed into an input field? (So any number 0-9)

Col <input type="text" value='tiledata.col' ng-model="tiledata.col" class="input-col" id="aeS-Col" ng-click="vm.setInputCol()">
Kode_12
  • 4,506
  • 11
  • 47
  • 97

4 Answers4

0

yes ..we can do by

       document.getElementById('aeS-Col').onkeydown = function(e){};

codepen url for reference http://codepen.io/nagasai/pen/zBrLEj

Naga Sai A
  • 10,771
  • 1
  • 21
  • 40
0

Well event can be fired on change, but does not depend on the value, but you can check within the onchange event handler function to check for the entered character :

element.addEventListener("keydown", function(event) {
    var character = keysight(event).char
    //Check if this is a number (eg: using regex)
    //Call function required.
}

In your code change the ng-click to ng-click=vm.setInputCol(event) and then in the function do the above check and return if not true is one way of doing it.

Arathi Sreekumar
  • 2,544
  • 1
  • 17
  • 28
0

Since it seems you are using angular, you can check like this as well.

$scope.vm.setInputCol = function(){

      if(typeof $scope.tiledata.col === typeof 1)
      {
        alert("Do your stuff");
      }
      else
      {
        return;
      };
    };
Deep
  • 9,594
  • 2
  • 21
  • 33
0

The .onkeypress() would be the preferred method for doing this. onkeypress vs onkeydown

In theory, the keydown and keyup events represent keys being pressed or released, while the keypress event represents a character being typed. The implementation of the theory is not same in all browsers.

So example (in jQuery):

$( "#aeS-Col" ).onkeypress( function( key )
{
    if( key.test( /[0-9]/ ) )
        callSuperFunction();
}
Community
  • 1
  • 1
Aaron
  • 428
  • 5
  • 14