If the email field is not left empty, then the re-email field must be marked as 'required'. In order to achieve this functionality, I have implemented conditional validators for the re-email field using the code snippet below:
HTML
<div class="row">
<div class="form-group col-xs-6">
<label class="control-label">Email Address:</label>
<input type="text" class="form-control" (blur)="reEnterEmail()" [ngClass]="{ 'quote-has-error' : startPageForm.controls['email'].invalid && startPageForm.controls['email'].touched}"
formControlName="email">
<label class="text-danger" *ngIf="startPageForm.controls['email'].hasError('pattern') && startPageForm.controls['email'].touched">Email is invalid</label>
</div>
<div class="form-group col-xs-6">
<label class="control-label">Re-enter Email Address :</label>
<input type="text" class="form-control" (blur)= "checkReEmail()" [ngClass]="{ 'quote-has-error' : startPageForm.controls['reEmail'].invalid && startPageForm.controls['reEmail'].touched}"
formControlName="reEmail" id="reEmailId">
<label class="text-danger" *ngIf="startPageForm.controls['reEmail'].touched && message == 'false' &&
this.startPageForm.controls.email.value != this.startPageForm.controls.reEmail.value" >Email is invalid</label>
</div>
</div>
Component
this.startPageForm = new FormGroup({
firstName : new FormControl( '', [Validators.required]),
middleName : new FormControl( '', [] ),
lastName : new FormControl( '', [Validators.required] ),
suffix : new FormControl( '', [] ),
dateOfBirth : new FormControl( this.dateOfBirth, [Validators.required, DateValidator, AgeValidator] ),
gender : new FormControl( this.genderEnumConstants[0].value, [] ),
maritalStatus : new FormControl( this.maritalStatusEnumConstants[0].value, [] ),
aprtUntNumber : new FormControl( '', [] ),
address : new FormControl( '', [] ),
city : new FormControl( '', [Validators.required] ),
stateId : new FormControl( '', [Validators.required] ),
zipcode : new FormControl( '', [Validators.required, Validators.maxLength(5)] ),
isMilitaryAddress : new FormControl( false, [] ),
mobileNo : new FormControl( '', [Validators.required] ),
homeNo : new FormControl( '', [] ),
email : new FormControl( '', [ Validators.pattern("^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,4}$")] ),
reEmail : new FormControl( '', [] ),
notificationMethod : new FormControl( this.notificationMethodEnumConstants[0].value, [] ),
policyTerm : new FormControl( this.policyTermEnumConstants[0].value, [] ),
effectiveDate : new FormControl( effectiveDate, [DateValidator, MinDateValidator] ),
});
reEnterEmail() {
this.startPageForm.controls['reEmail'].setValidators(this.setRequired());
}
setRequired() {
if(this.startPageForm.controls.email.value != null && this.startPageForm.controls.email.value != '') {
return [Validators.required];
} else {
return [];
}
}
Upon triggering the (blur)="reEnterEmail()"
event on the email field, I am assigning validators to the reEmail field. However, an error message was encountered:
Uncaught ReferenceError: Validators is not defined(…)