Skip to content Skip to sidebar Skip to footer

How To Change Ng-keypress To Ng-change Function

Here I have a text box. In that field There is a validation that need to allow only numeric,alphabets and spanish characters. For that I found a function in javascript. That functi

Solution 1:

ng-change must be used with ng-model. So, add ng-model to the input field.

Read more about ng-changehere

EDIT :

As Alexis Toby said, ng-change does not have $event. Remove it. It will work.

Solution 2:

I think you can use a directive instead. Please find the following code update it as you need, I hope this will help. Please add the key codes for Backspace and delete in both. Thank you

<input type="text"  ng-model="name" documentvalue="{{documentTypeNumbe}}" filtered-character>

directive('filteredCharacter', function () {
  returnfunction (scope, element, attrs) {

    element.bind("keydown paste", function (event) {
      //console.log($.inArray(event.which,keyCode));var keyCode = [];
      if (attrs.documentvalue == 1 || attrs.documentvalue == 2) {
        keyCode = [65, 91, 159]; // Please add all the required key codes
      } elseif (attrs.documentvalue == 6) {
        keyCode = [67, 93, 161]; // Please add all the required key codes
      }
      if ($.inArray(event.which, keyCode) === -1) {
        scope.$apply(function () {
          scope.$eval(attrs.filteredCharacter);
          event.preventDefault();
        });
        event.preventDefault();
      }

    });
  };
});

Solution 3:

Try calling the same the function using "ng-keydown". Follow the link to this plunker

<input type="text" ng-model="name" ng-keydown="getPatternForAlphebet($event,$index)">

Post a Comment for "How To Change Ng-keypress To Ng-change Function"