jeudi 17 janvier 2019

Angular js directive that would allow alpha characters and would include capslock and space

Below is my angular js directive that would only allow alphabetical characters but the issue is that it disable capslock/space. The goal is not allowing special characters and numbers as input only alphabet but would not disable capslock/space/shift cause in the app the user can input his name in capital letter.Any idea?. I am using directive compared to ng-pattern library.

app.directive('validEn', function () {
    return {
        require: '?ngModel',
        link: function (scope, element, attrs, ngModelCtrl, SweetAlert) {
            if (!ngModelCtrl) {
                return;
            }

            ngModelCtrl.$parsers.push(function (val) {
                var clean = val.replace(/[^a-z]+/g, '');
                console.log("sfdsfd")
                if (val !== clean) {
                    ngModelCtrl.$setViewValue(clean);
                    ngModelCtrl.$render();
                }
                return clean;
            });

            element.bind('keypress', function (event) {
                if (event.keyCode === 32) {
                    event.preventDefault();
                }
            });
        }
    };
});

Aucun commentaire:

Enregistrer un commentaire