简体   繁体   中英

Can not validate password field with reg exp using angular.js

I need to validate my password field with some special character.This field should contain at least one number,upper case ,lowercase letter and underscore and these are mandotary.I am using ng-pattern and ng-message but failed to do this.I am explaining my code below.

<span class="input-group-addon ndrftextwidth text-right" style="width:180px">Password :</span>
<div ng-class="{ 'has-error': billdata.pass.$touched && billdata.pass.$invalid }">
<input type="{{inputType}}" name="pass" id="contactno" class="form-control" placeholder="password" ng-model="password" ng-minlength="8" ng-pattern="/[a-zA-Z][0-9][a-zA-Z0-9]{3}/." >
</div>
</div>
<div class="help-block" ng-messages="billdata.pass.$error" ng-if="billdata.pass.$touched">
<p ng-message="minlength" style="color:#F00;">This field is too short.The min length of your password should be 8.</p>
<p ng-message="pattern" style="color:#F00;">This field needs the special character like number,upper case,lower case letter,underscore.</p>
</div> 

Here i am not getting any error message.Please help me.

Field should contain at least one number,upper case ,lowercase letter and underscore and these are mandotary.

You need to use lookahead based regex.

ng-pattern="/^(?=.*[A-Z])(?=.*\d)(?=.*[a-z]).*_.*/"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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