Ng-Keydown-8

 

In this example of Ng-Keydown, We have added validation on key down of text boxes. Textbox is used to get the age of user so if user will enter value other then numeric then that will be invalid value. So we added validation on textbox keydown using Ng-Keydown

 
 
 
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Welcome to LearnKode - A code learning platform</title> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"></script> </head> <body ng-app="app"> <div ng-controller="myController"> <h3>Numeric TextBox Validation on Keydown</h3> Age: <input type="text" ng-keydown="isNumeric($event)" /> </div> <script> var app = angular.module("app", []); app.controller('myController', ['$scope', function ($scope) { $scope.isNumeric = function (e) { if (!((e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 96 && e.keyCode <= 105) || (e.keyCode == 8))) { return e.preventDefault(); } } }]); </script> </body> </html>
Output