component.html
<div class="form-group">
<label>Enter mobile</label>
<input type="text" class="form-control" formControlName="mobile" ><br>
</div>
<div *ngIf="userGroup.controls.mobile.invalid && (userGroup.controls.mobile.dirty || userGroup.controls.mobile.touched)">
<div *ngIf="userGroup.controls.mobile.errors.required">
Mobile number cannot be blank
</div>
<div *ngIf="userGroup.controls.mobile.errors.pattern">
Mobile number should be 10 digits only
</div>
</div>
component.ts
userGroup:FormGroup;
ngOnInit() { this.userGroup = this.fb.group({
mobile:['',Validators.required,Validators.pattern(/^[0-9]{10}$/)]
});
}
For the blank it is working perfectly but for pattern it is not showing any error