2

How to disable special characters in angular js input tag. Only allow alphanumeric just like we use

<input type="text" ng-trim="false" style="text-transform: uppercase" ng-pattern="/^[a-zA-Z0-9]*$/" class="form-text" id="pan_card_number" name="pan_card_number" ng-minlength="10" maxlength="10" required ng-model="registration.newTSP.panCardNumber">
3
  • <input type="text" ng-trim="false" style="text-transform: uppercase" ng-pattern="/^[a-zA-Z0-9]*$/" class="form-text" id="pan_card_number" name="pan_card_number" ng-minlength="10" maxlength="10" required ng-model="registration.newTSP.panCardNumber"> Commented Aug 1, 2016 at 9:12
  • any one please help me. how can i do it. I already using ng-pattern="/^[a-zA-Z0-9]*$/" . But it allow in input text . i want to dissallow special character. Thanks for Advance Commented Aug 1, 2016 at 9:13
  • 1
    The actual pancard regex is 5 chars 4 digits and a trailing char. Have updated an answer below. Commented Aug 1, 2016 at 9:13

5 Answers 5

8

you can use Regex with Ng-pattern and Display the message through ng-message

$scope.useOnlySpecialCharacters = /^[a-zA-Z0-9]*$/;

<input type="text" ng-model="specialcharacters" 
ng-pattern="useOnlySpecialCharacters" />

show message through ng-message

<div ng-message="pattern"> Please Enter AlphaNumeric </div>

OR

Best Option is to use Directives

app.directive('noSpecialChar', function () {
  return {
    require: 'ngModel',
    restrict: 'A',
    link: function (scope, element, attrs, modelCtrl) {
      modelCtrl.$parsers.push(function (inputValue) {
        if (inputValue == null) {
          return '';
        }

        var cleanInputValue = inputValue.replace(/[^\w\s]/gi, '');
        if (cleanInputValue != inputValue) {
          modelCtrl.$setViewValue(cleanInputValue);
          modelCtrl.$render();
        }

        return cleanInputValue;
      });
    }
  }
});

LINK

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

2 Comments

its so more complicated . I used ng-pattern = "/[A-Z]{5}\d{4}[A-Z]{1}/i" its a proper PAN card Number (regularExpression) .. ans by liberalTGM
And to allow umlauts and other accented characters (Diacritics), use /[^A-zÀ-ÿ\s]/gi in the directive.
3

use ng-pattern="/[A-Z]{5}\d{4}[A-Z]{1}/i" in your HTML input tag

1 Comment

2

use the following

Controller

$scope.panCardRegex = '/[A-Z]{5}\d{4}[A-Z]{1}/i';

HTML

<input type="text" ng-model="abc" ng-pattern="panCardRegex" />

5 Comments

Thanks Man .. Its Working (You are Awsome)
@ved-prakash Can you accept the answer, if this is working.
Yes i accepted this answer and also i put your code in my project ****Thanks a lot*****
How to disable space bar also
<input type="text" ng-trim="false" style="text-transform: uppercase" ng-pattern="/[A-Z]{5}\d{4}[A-Z]{1}/i" class="form-text" id="pan_card_number" name="pan_card_number" ng-minlength="10" maxlength="10" required ng-model="registration.newTSP.panCardNumber"> <span class="bar"></span> <label>Company PAN card number (<span style="color: red;">*</span>) </label>
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
          };      
      });


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

Comments

1

set pattern to allow only alphanumeric

/^[a-z0-9]+$/i

Comments

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.