Restrict Special Characters in HTML & AngularJs

后端 未结 3 1447
你的背包
你的背包 2021-01-14 05:24
 ` ~ ! @ # $ % ^ & * ( ) _ + = { } | [ ] \\ : \' ; \" < > ? , . /

I want to restrict the above mentioned special characters and numbers i

3条回答
  •  青春惊慌失措
    2021-01-14 05:38

    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:    
        
    

提交回复
热议问题