【问题标题】:Restrict Special Characters in HTML & AngularJs限制 HTML 和 AngularJs 中的特殊字符
【发布时间】:2017-02-21 16:20:39
【问题描述】:
 ` ~ ! @ # $ % ^ & * ( ) _ + = { } | [ ] \ : ' ; " < > ? , . /

我想在输入文本字段中限制上述特殊字符和数字。我用了

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

限制特殊字符。这种模式会阻止所有特殊字符。当我想输入名称“Pérez Gil”时遇到问题我不想限制其他语言文本。

【问题讨论】:

    标签: html angularjs


    【解决方案1】:

    更新:

    我认为 $parsers 是这里最好的选择。查看更新的代码和 plunker。

    控制器

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

    模板

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

    这是一个关于 Plunker 的更新示例

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

    旧答案:

    由于您已经有了要限制的字符列表,您可以在 ng-pattern 表达式中将它们拼写出来,例如:

    控制器

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

    模板

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

    这是一个关于 Plunker 的工作示例

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

    【讨论】:

    • @Warrior 抱歉,我没有正确理解要求。我更新了我的答案和 plunker 以满足您的需求。让我知道这是否适合您
    【解决方案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>
    

    【讨论】:

    • 您的答案确实显示,如何将正则表达式与 Angular 一起使用,但问题清楚地表明 OP 已经使用的正则表达式不是 OP 想要的值。因此,只需复制并粘贴给定的正则表达式不是 真正的 解决方案,请编辑您的答案以满足 真正 回答问题的要求!如果您想不出除了您发布的其他解决方案,请考虑删除您的答案——上面的布雷松斯。
    【解决方案3】:
    <!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>
    

    【讨论】:

    • 请为您的回答提供更多解释,为什么这会帮助 OP 解决他的问题?
    猜你喜欢
    • 1970-01-01
    • 2018-10-25
    • 1970-01-01
    • 1970-01-01
    • 2013-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多