简体   繁体   English

通过ng-pattern限制输入字段中的几个特殊字符,如%,&和“

[英]Restricting few special characters like %,& and " in input field by ng-pattern

I have a password where It should be minimum 8 character with alpha numeric and symbol except &,",% .I given ng-pattern and restricted few symbols. but when I type & too it is accepting. This ng-Pattern normally works. when you enter & and $ it is accepting both. Whereas it should not allow &. thats my point 我有一个密码,该密码应至少由8个字符组成,并带有字母数字和符号,除了&,“,%。我给出了ng-pattern并限制了几个符号。但是当我键入&时,它也可以接受。此ng-Pattern通常可以使用。当您输入&和$时,两者都接受。但是它不应允许&。这就是我的观点

<div class="form-group fields col-xs-12 col-sm-6 col-md-4 col-lg-4" ng-class="{'has-error' : (submitted || tForm.password.$dirty || tForm.submitted) && tForm.password.$invalid }">
    <input type="password" name="password" placeholder ="Password*" class="form-control1" autocomplete="off"  ng-model="model.password" ng-minlength="8" ng-maxlength="20" required ng-pattern="/(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$^*)(._-])(?=.*[^a-zA-Z._-])(?=.*[^a-zA-Z._-])/"  />
<div ng-show="!tForm.password.$error.required && (tForm.password.$error.minlength || tForm.password.$error.maxlength) && tForm.password.$dirty" class="help-block">Passwords must be between 8 and 20 characters.</div>
       <div ng-show="!tForm.password.$error.required && !tForm.password.$error.minlength && !tForm.password.$error.maxlength && tForm.password.$error.pattern && tForm.password.$dirty" class="help-block">Must contain one lower &amp; uppercase letter, number and one non-alpha character (except %,",")</div>
      <div ng-show="(submitted && tForm.password.$error.required) " class="help-block">password is required.</div>
       </div>

Check this one. 检查这个。 This is working fine for me, Not accepting & and %. 这对我来说很好,不接受&和%。

<!DOCTYPE html>
<html ng-app="myapp">

  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
  </head>
  <body>
    <div ng-controller="myCtrl">
    <form name="myForm" ng-submit="onSubmit()">
    <input type="text" ng-model="price" name="price_field" 
           ng-pattern="/(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$^*)(._-])(?=.*[^a-zA-Z._-])(?=.*[^a-zA-Z._-])/" required>
    <span ng-show="myForm.price_field.$error.pattern">Not a valid password</span>
    <span ng-show="myForm.price_field.$error.required">This field is required!</span>
    <input type="submit" value="submit"/>
    </div>
  </form>
  <script>
    angular.module('myapp',[])
    .controller('myCtrl',function($scope){

    })
  </script>
  </body>

</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM