正则表达式无法正常工作,这里出了什么问题? [英] regular expression not working as expected, what gone wrong here?

查看:42
本文介绍了正则表达式无法正常工作,这里出了什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于输入密码的文本框,该框至少应接受1个字母和1个特殊字符.且长度不得少于8个字符,且不应大于15个字符.但是现在它接受了超过15个字符.哪里出错了?

I have a text box for password which should accept 1 letter 1 special character at least. and it should not be less than 8 character and should not be greater than 15 character . but now its accepting more than 15 character . where it gone wrong?

http://jsfiddle.net/u1fm0jLo/243/

 ng-pattern="/((?=.*\W).{8,15}$)/"

推荐答案

尝试一下

ng-pattern="/^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$&*])[A-Za-z\d@$&*]{8,15}$/"

(?=.* [A-Za-z])-声明一个字符串至少包含一个字母;

(?=.*[A-Za-z]) - Assert a string has at least one Alphabet;

(?=.* \ d)-声明一个字符串至少包含一个数字;

(?=.*\d) -Assert a string has at least one number;

(?=. [@ $& ])-断言一个字符串至少具有此字符之一;

(?=.[@$&]) - Assert a string has at least one of this character;

[A-Za-z \ d @ $& *] {8,15}-字符(仅数字和字母)长度应该在8到15之间

[A-Za-z\d@$&*]{8,15} - Characters (Only numbers and letters) length should be between 8 and 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>

这篇关于正则表达式无法正常工作,这里出了什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆