2017-03-16 36 views
0

我有一个密码,它应该是最少8个字符,带有字母数字和符号,除了&,“,%.I给定了ng-pattern和restricted几个符号。但是当我输入&时它也接受。这个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])(?=.*[[email protected]#$^*)(._-])(?=.*[^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> 

回答

1

检查这一项。这对我来说很好,不接受&和%。

<!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])(?=.*[[email protected]#$^*)(._-])(?=.*[^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> 
+0

嗨,这将工作。但是,当你输入&和$时,它同时接受。而它不应该允许&。这是我的观点。 –

+0

为您编辑代码。它为我工作。 –