首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >限制HTML和AngularJ中的特殊字符

问限制HTML和AngularJ中的特殊字符
EN

Stack Overflow用户
提问于 2016-10-13 02:25:55
回答 3查看 22.5K关注 0票数 3
代码语言:javascript
复制
 ` ~ ! @ # $ % ^ & * ( ) _ + = { } | [ ] \ : ' ; " < > ? , . /

我想在输入文本字段中限制上面提到的特殊字符和数字。我使用了

代码语言:javascript
复制
ng-pattern="/^[a-zA-Z ]*$/"

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-10-13 03:07:22

更新:

我认为$parsers是最好的选择。请参阅更新的代码和柱塞器。

控制器

代码语言:javascript
复制
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
      };      
  });

模板

代码语言:javascript
复制
<input type="text" ng-model="model" id="input" name="input" my-directive />

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

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

旧答案

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

控制器

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

模板

代码语言:javascript
复制
<input type="text" ng-model="model" id="input" name="input" ng-pattern="regex" />

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

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

票数 11
EN

Stack Overflow用户

发布于 2017-10-26 20:06:57

代码语言:javascript
复制
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>
票数 2
EN

Stack Overflow用户

发布于 2017-04-29 02:14:59

代码语言:javascript
复制
<!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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40005810

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档