0

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.

1 Answer 1

2

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,}"

Sign up to request clarification or add additional context in comments.

2 Comments

It's a good practice to remove dot-stars whenever it's possible ^(?=[^a-z]*[a-z])(?=[^A-Z]*[A-Z])(?=\D*\d).{8,}.
@revo agreed, I was only pointing out the problem in his original regex.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.