4
 ` ~ ! @ # $ % ^ & * ( ) _ + = { } | [ ] \ : ' ; " < > ? , . /

I want to restrict the above mentioned special characters and numbers in the input text field. I used the

ng-pattern="/^[a-zA-Z ]*$/"

to restrict the special characters. This pattern is blocking all the special characters. I am facing issue when I want to enter name "Pérez Gil" I don't want to restrict other language text.

3 Answers 3

12

Updates:

I think $parsers is the best options here. See the updated code and plunker.

Controller

angular.module('ngPatternExample', [])
  .controller('ExampleController', ['$scope', function($scope) {
    $scope.regex = /^[^`~!@#$%\^&*()_+={}|[\]\\:';"<>?,./1-9]*$/;
  }])
  .directive('myDirective', function() {
     function link(scope, elem, attrs, ngModel) {
          ngModel.$parsers.push(function(viewValue) {
            var reg = /^[^`~!@#$%\^&*()_+={}|[\]\\:';"<>?,./1-9]*$/;
            // if view values matches regexp, update model value
            if (viewValue.match(reg)) {
              return viewValue;
            }
            // keep the model value as it is
            var transformedValue = ngModel.$modelValue;
            ngModel.$setViewValue(transformedValue);
            ngModel.$render();
            return transformedValue;
          });
      }

      return {
          restrict: 'A',
          require: 'ngModel',
          link: link
      };      
  });

Template

<input type="text" ng-model="model" id="input" name="input" my-directive />

Here's a updated example on Plunker

https://plnkr.co/edit/eEOJLi?p=preview

Old Answers:

Since you already have a list of characters that you want to restrict, you can spell them out in the ng-pattern expression like:

Controller

angular.module('ngPatternExample', [])
  .controller('ExampleController', ['$scope', function($scope) {
      $scope.regex = /^[^`~!@#$%\^&*()_+={}|[\]\\:';"<>?,./1-9]*$/;
  }]);

Template

<input type="text" ng-model="model" id="input" name="input" ng-pattern="regex" />

Here's a working example on Plunker

https://plnkr.co/edit/eEOJLi?p=preview

Sign up to request clarification or add additional context in comments.

1 Comment

@Warrior Sorry I didn't understand the requirement correctly. I updated my answer and plunker to fit your needs. Let me know if this works for you
2
Use Directives to restrict Special characters: 

 angular.module('scPatternExample', [])
      .controller('scController', ['$scope', function($scope) {
      }])
    .directive('restrictSpecialCharactersDirective', function() {
         function link(scope, elem, attrs, ngModel) {
              ngModel.$parsers.push(function(viewValue) {
                var reg = /^[a-zA-Z0-9]*$/;
                if (viewValue.match(reg)) {
                  return viewValue;
                }
                var transformedValue = ngModel.$modelValue;
                ngModel.$setViewValue(transformedValue);
                ngModel.$render();
                return transformedValue;
              });
          }

          return {
              restrict: 'A',
              require: 'ngModel',
              link: link
          };      
      });

In Html:    
    <input type="text" ng-model="coupon.code" restrict-Special-Characters-Directive>

1 Comment

your answer does indeed Show, how to use regex with angular, but the question clearly stated that the regular expression OP already uses does not the values OP wants. so just copy and pasting the given regex is not a real solution please edit your answer to fulfill the requirement to really answer the question! if you cant think of an solution other than you posted, please think about deleting the your answer -- breasons above.
0
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body ng-app="myApp">
<div ng-controller="myCtrl">

  <input type="text"  ng-change="Check(myValue)" ng-model="myValue" />
  <p ng-show="test">The Special Character not accept.</p>
</div>

<script>
  angular.module('myApp', [])
    .controller('myCtrl', ['$scope', function($scope,ngModel) {


      $scope.Check= function(x) {
        var reg = /^[^`~!@#$%\^&*()_+={}|[\]\\:';"<>?,./]*$/;

              if (!x.match(reg)) {
              $scope.myValue = x.substring(0, x.length-1);
              $scope.test=true;

              }
              else
              {
               $scope.test=false;
              }
      };
    }]);
</script>
</body>
</html>

1 Comment

please provide more explanation to your answer, why should this help the OP with his issue?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.