我有一个密码文本框,至少应该接受1个字母1的特殊字符。它不应少于8个字符,不应超过15个字符。但现在它接受超过15个字符。哪里出错了?
http://jsfiddle.net/u1fm0jLo/243/
ng-pattern="/((?=.*\W).{8,15}$)/"
答案 0 :(得分:1)
试试这个
ng-pattern="/^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$&*])[A-Za-z\d@$&*]{8,15}$/"
(?=。* [A-Za-z]) - 断言一个字符串至少有一个字母;
(?=。* \ d)-Assert一个字符串至少有一个数字;
(?=。 [@ $& ]) - 断言字符串至少包含一个此字符;
[A-Za-z \ d @ $& *] {8,15} - 字符(仅限数字和字母)长度 应该在8到15之间
var app = angular.module("app", []);
app.controller("ctrl", function($scope) {
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
<label>Password</label>
<form name="form">
<input type="password" style="width: 600px" class="form-control" name="Password" id="Password" ng-pattern="/^(?=.*[A-Za-z])(?=.*[@$&*])[A-Za-z@$&*]{8,15}$/" ng-model="User.Password" ng-minlength="7" ng-class="submitted?'ng-dirty':''" required />
<br/>
<span class="error" ng-show=" (form.Password.$dirty || submitted) && form.Password.$error.required">Required</span>
<span class="error" ng-show=" (form.Password.$dirty || submitted) && form.Password.$error.minlength">Mini 8 characters</span>
<span class="success" ng-show=" (form.Password.$dirty || submitted) && form.Password.$error.pattern">Invalid</span>
</form>
</div>
答案 1 :(得分:0)
这是你期待的吗? \W
匹配任何非单词字符。与[^a-zA-Z0-9_]
/((?=.*\d)(?=.*[a-zA-Z])(?=.*\W).{8,15}$)/
答案 2 :(得分:0)
尝试这种模式
ng-pattern="^(?=.*[a-zA-z])(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{8,15}"
(?=。* [a-zA-z]) - &gt;这将检查至少1个字母
(?=。 [$ @ $!%?&amp;]) - &gt;这将检查至少1个特殊字符
[A-Za-z \ d $ @ $!%*?&amp;] {8,15} - &gt;检查总长度是> 8和&lt; 15