How to limit special characters in angularjs text box

I am learning AngularJS and am currently trying form validation for AngularJS. I want to limit special characters in a text field, that is, if the user enters any special characters, he should not allow or display them in the text field. If anyone knows the solution, please comment.

enter image description here

+4
source share
2 answers

. NgModelController $parsers, , ( ) ng-model .

. .

  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;
        });
      }
    }
  });

.

. no-special-char

: <input type="text" no-special-char ng-model="username">

. Plnkr

+20

ng-pattern /^[a-zA-Z0-9]*$/.

<input type="text" ng-pattern="/^[a-zA-Z0-9]*$/">

.

+14

Source: https://habr.com/ru/post/1618398/


All Articles