Sujay U N
Sujay U N

Reputation: 5340

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 : enter image description here

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.

Upvotes: 0

Views: 799

Answers (1)

Diadistis
Diadistis

Reputation: 12174

You have to capture something in your regex, you're only using positive look-ahead groups. Just change the length part like this :

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

Upvotes: 2

Related Questions