如何根据一定的标准在angularjs中使用正则表达式添加密码验证? [英] How to add password validation using regular expression in angularjs according to certain criterion?
问题描述
我想验证用户输入的密码是否符合以下条件:
I want to validate password entered by user for following criteria :
密码长度至少应为8个字符,并且应包含一个数字,一个字符和一个特殊字符。
为此,我使用了以下正则表达式:
For it I used following regular expression :
^(?=.*[A-Za-z])(?=.*\d)(?=.*[$@$!%*#?&])[A-Za-z\d$@$!%*#?&]{8,}$
我在我的angularjs代码中尝试了这个表达式,如下所示:
I tried this expression in my angularjs code as below :
<md-input-container class="md-block" style="margin-top:0px;">
<label>Password</label> <md-icon
md-svg-src="/images/icons/ic_lock_black_24px.svg" class="name"></md-icon>
<input type="password" ng-model="newUser.userPassword"
name="userPassword" required
ng-pattern="^(?=.*[A-Za-z])(?=.*\d)(?=.*[$@$!%*#?&])[A-Za-z\d$@$!%*#?&]{8,}$">
<div
ng-messages="registerForm.userPassword.$error">
<div ng-message="pattern">Password should be atleast 8 characters long
and should contain one number,one character and one special
character</div>
<div ng-message="required">Password should be atleast 8 characters
long and should contain one number,one character and one special
character</div>
</div>
</md-input-container>
在上面的代码中,当密码字段为空并且焦点丢失时,会显示错误消息。如果用户输入的密码不符合我提到的标准,则不会显示错误消息。
In my above code the error message displays when the password field is blank and focus is lost. If user enters a password which doesn't satisfy the criteria I mentioned the error message doesn't show up.
我该如何解决此问题?有人请帮助我。
How should I fix this issue? Somebody please help me.
推荐答案
据我了解你的问题。检查你的控制台它可能因词汇错误而中断。
As far as i understood your problem. Check your console It might be broken on lexical errors.
你在开始和结束时忘了斜杠
图案。
请使用 ng-pattern =/ ^(?=。* [A-Za-z])(?=。* \ d)(?=。* [$ @ $ !%*#?&])[A-Za-z\d $ @ $!%*#?&] {8,} $ /
You forgot slash
at start and at end of ng-pattern.
Please use ng-pattern="/^(?=.*[A-Za-z])(?=.*\d)(?=.*[$@$!%*#?&])[A-Za-z\d$@$!%*#?&]{8,}$/"
我创建了一个可能对你有用的插件。
https://plnkr.co/edit/qCjp6a?p=preview
I have created a plunk which might help you. https://plnkr.co/edit/qCjp6a?p=preview
这篇关于如何根据一定的标准在angularjs中使用正则表达式添加密码验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!