Biology, asked by divu879, 1 year ago

on keypress does not allow alphabets angular 2

Answers

Answered by anushasahu
0
1.var mainApp = angular.module("mainApp", ['ngRoute', 'ngResource']);
mainApp.directive('allowPattern', [allowPatternDirective]);

function allowPatternDirective() {
    return {
        restrict: "A",
        compile: function (tElement, tAttrs) {
            return function (scope, element, attrs) {
                // I handle key events
                element.bind("keypress", function (event) {
                    var keyCode = event.which || event.keyCode; // I safely get the keyCode pressed from the event.
                    var keyCodeChar = String.fromCharCode(keyCode); // I determine the char from the keyCode.

                    // If the keyCode char does not match the allowed Regex Pattern, then don't allow the input into the field. if (!keyCodeChar.match(new RegExp(attrs.allowPattern, "i"))) {
                        event.preventDefault();
                        return false;
                    }

                });
            };
        }
    };
}


2. <input type="text" name="jjj"
               allow-pattern="^[a-zA-Z ]*$" />
Similar questions