I understand the angularJs directive in detail. I am currently using it to prevent the user from entering special characters.
here is the code
HTML
<input type="text" no-special-char ng-model="vm.customTag" class="form-control" value="" />
AngularJS Directive
app.directive('noSpecialChar', function () { return { require: 'ngModel', restrict: 'A', link: function (scope, element, attrs, modelCtrl) { modelCtrl.$parsers.push(function (inputValue) { if (inputValue == null) return '' cleanInputValue = inputValue.replace(/[^\w\s]/gi, ''); if (cleanInputValue != inputValue) { modelCtrl.$setViewValue(cleanInputValue); modelCtrl.$render(); } return cleanInputValue; }); } } });
here are two things i want
(1) the user can enter a minus / dash '-' , which is not happening right now, how can I change my /[^\w\s]/gi , which allows the user to enter a sign (depression / minus sign).
(2) The above functionality restricts the user from entering any special character, but when the user enters a special character, I want to display a red warning and special characters are not allowed , how can I do this?
Any help is appreciated !!!
thanks
source share