Angular js allow alphabetical caharacters and special characters but not numbers

103 Views Asked by At

My directive is below. What i want is that to allow alphabetical caharacters and special characters but not numbers. How can we changed that based on my code below.Thank you.

   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|^A-z|^\s]+/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();
                    }
                });
            }
        };
    });
1

There are 1 best solutions below

0
On BEST ANSWER

You need to change your regex statement so that it replaces any numerics with empty string, as such:

var clean = val.replace(/\d*/g, '');