简体   繁体   中英

Password Validation error in Angular Reactive Form Validator

Using Angular Reactive Form to validate password which as to match Password with atleast :

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

Using Reg Exp :

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

Html Code :

<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 :

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/

The same reg exp shows valid for the requirments. But its invalid in 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,}"

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