[英]How to add password validation using regular expression in angular 8 with some conditions?
I want to validate a password entered by the user for the following criteria:我想验证用户输入的密码是否符合以下条件:
Password should be at least 8 characters as min and 20 as max and should contain one number, one capitalized character, and one special character within this only, ./<>?;':"[]{}\|.@#$%^&*(-=_+ (.
密码至少应包含最少 8 个字符和最多 20 个字符,并且应仅包含一个数字、一个大写字符和一个特殊字符,. ./<>?;':"[]{}\|.@#$%^&*(-=_+ (.
:"[]{}\|.@#$ ./<>?;':"[]{}\|.@#$%^&*(-=_+ (.
For it I used following regular expression:为此,我使用了以下正则表达式:
Password:
['', [Validators.pattern('(?=. [az])(?=. [AZ])(?=. [0-9])(?=. [$@$!% #?^&+=,.-])[A-Za-z\d$@$!% #?^&+=,.-].{8,}')]]`` Password:
['', [Validators.pattern('(?=. [az])(?=. [AZ])(?=. [0-9])(?=. [$@$!% #? ^&+=,.-])[A-Za-z\d$@$!% #?^&+=,.-].{8,}')]]``
It is called pattern validation.它被称为模式验证。 You can check this link https://angular.io/api/forms/PatternValidator to see angular example.您可以查看此链接https://angular.io/api/forms/PatternValidator以查看 angular 示例。 When you put "pattern" attribute (takes regEx) to element it will work as a validator.当您将“模式”属性(采用正则表达式)添加到元素时,它将作为验证器工作。
<input name="firstName" ngModel pattern="[a-zA-Z ]*">
You can use the pattern like:您可以使用如下模式:
/^(?=.*[0-9])(?=.*[a-z][A-Z])(?=.*[//,.?;<>:!@#$%^&*(-=_+)|{}\[\]])([a-zA-Z0-9//,.?;<>\':\"!@#$%^&*(-=_+)|{}\[\]]{8,20})$/
^
The password string will start this way ^
密码字符串将以这种方式开始(?=.*[AZ])
The string must contain at least 1 uppercase alphabetical character (?=.*[AZ])
字符串必须至少包含 1 个大写字母字符(?=.*[0-9])
The string must contain at least 1 numeric character (?=.*[0-9])
字符串必须至少包含 1 个数字字符(?=.*[//,.?;<>':"!@#$%^&*(-=_+)|{}\[\]])
The string must contain at least one special character (?=.*[//,.?;<>':"!@#$%^&*(-=_+)|{}\[\]])
字符串必须至少包含一个特殊字符(?=.{8,20})
The string must be eight characters to Maximum 20 characters (?=.{8,20})
字符串必须为 8 个字符到最多 20 个字符In angular 8 you can use在 angular 8 你可以使用
<input name="password" ngModel pattern="/^(?=.*[0-9])(?=.*[a-z][A-Z])(?=.*[//,.?;<>:!@#$%^&*(-=_+)|{}\[\]])([a-zA-Z0-9//,.?;<>\':\"!@#$%^&*(-=_+)|{}\[\]]{8,20})$/">
I think this can help..我认为这可以帮助..
You can add your pattern by following two steps:您可以通过以下两个步骤添加您的模式:
In HTML file在 HTML 文件中
<div *ngIf="f.Password.errors.required"> This field is mandatory</div> <div *ngIf="f.Password.errors.minlength"> Must be at least 8 digits</div> <div *ngIf="f.Password.errors.maxlength"> Must be Max 20 digits</div> <div *ngIf="f.Password.errors.pattern"> Must contain at least 1 Special Character!</div>
In ts file在 ts 文件中
Password: [null, [ Validators.required, Validators.minLength(8), Validators.maxLength(20), Validators.pattern(/[;@#$%^&*()_+-=[]{}:',"\|.?<>/?][a-zA-Z0-9 ]/) ]]密码:[null, [ Validators.required, Validators.minLength(8), Validators.maxLength(20), Validators.pattern(/[;@#$%^&*()_+-=[]{}:' ,"\|.?<>/?][a-zA-Z0-9 ]/) ]]
Run Demo - https://stackblitz.com/edit/angular-shhd1i运行演示- https://stackblitz.com/edit/angular-shhd1i
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.