简体   繁体   English

Angular反应形式验证器中的密码验证错误

[英]Password Validation error in Angular Reactive Form Validator

Using Angular Reactive Form to validate password which as to match Password with atleast : 使用Angular Reactive Form验证密码,以使密码与atleast匹配:

|*> 1 lowercase
|*> 1 uppercase
|*> 1 numeric
|*> 8 char longer

Using Reg Exp : 使用Reg Exp:

"^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.{8,})"

Html Code : HTML代码:

<form [formGroup]="NamFomNgs">

    <label>Email : 
        <input type="email" name="MylHtm" formControlName="MylNgs">
    </label><br>
        <div class="ErrMsgCls" *ngIf="(NamFomNgs.controls['MylNgs'].touched || NamFomNgs.controls['MylNgs'].dirty) && 
            !NamFomNgs.controls['MylNgs'].valid">
            <span *ngIf="NamFomNgs.controls['MylNgs'].errors.required">This field is required</span>
            <span *ngIf="NamFomNgs.controls['MylNgs'].errors.email">Enter valid email</span>
        </div><br>

    <label>Password : 
        <input type="text" name="PwdHtm" formControlName="PwdNgs">
    </label><br>
        <div class="ErrMsgCls" *ngIf="(NamFomNgs.controls['PwdNgs'].touched || NamFomNgs.controls['PwdNgs'].dirty) && 
            !NamFomNgs.controls['PwdNgs'].valid">
            <span *ngIf="NamFomNgs.controls['PwdNgs'].errors.required">This field is required</span>
            <span *ngIf="NamFomNgs.controls['PwdNgs'].errors.pattern">Enter valid password</span>
        </div><br>

    <button [disabled]="!NamFomNgs.valid">Submit</button>
</form>

TypeScript Code : TypeScript代码:

NamFomNgs:FormGroup;

constructor(private NavPkjVaj: ActivatedRoute, private HtpCncMgrVaj: HttpClient,private FomNgsPkjVaj: FormBuilder){

    this.NamFomNgs = FomNgsPkjVaj.group(
        {
            MylNgs:[null,Validators.compose([
                Validators.required,
                Validators.email ])],
            PwdNgs:[null,Validators.compose([
                Validators.required,
                Validators.pattern("^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.{8,})")])]
        });
}

Screen Shot of Output : 输出屏幕截图: 在此处输入图片说明

When I try with https://regex101.com/ 当我尝试使用https://regex101.com/时

The same reg exp shows valid for the requirments. 相同的reg exp显示对要求有效。 But its invalid in Angular. 但是它在Angular中无效。 Kindly help me with right way and to resolve this. 请以正确的方式帮助我并解决此问题。

You have to capture something in your regex, you're only using positive look-ahead groups. 您只需要在正则表达式组中捕获正则表达式中的内容即可。 Just change the length part like this : 只需像这样更改长度部分:

"^(?=.*[az])(?=.*[AZ])(?=.*[0-9]).{8,}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 6反应形式验证不适用于自定义验证器 - Angular 6 reactive form validation not working with custom validator Angular 5中具有“比较密码”验证的反应形式 - Reactive Form With “Compare Password” Validation in Angular 5 Angular 6和Ionic 3反应形式验证错误不显示 - Angular 6 and Ionic 3 reactive form validation error not display 自定义验证器在以角度 2 重置反应形式时抛出错误 - Custom Validator throw error upon resetting reactive form in angular 2 反应表单上的自定义验证器用于密码并确认密码匹配将未定义的参数导入 Angular 4 - Custom Validator on reactive form for password and confirm password matching getting undefined parameters into Angular 4 Angular 反应式表单在生产构建的验证条件上显示错误 - Angular Reactive form showing error on validation condition on production build jQuery表单验证器密码错误 - jQuery Form Validator Password error 向 Angular 反应式 Forms 添加确认密码验证? - Adding confirm password validation to Angular Reactive Forms? 使用反应形式的角度密码和确认密码验证 - password and confirmpassword validation in angular using reactive forms 如何为 angular 反应式表单中的表单值添加自定义唯一验证器? - How to add custom unique validator for form values in angular reactive form?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM