I have an input where user needs to type a longitude. I want to be able to display different error message when user types NaN
or Not-Precise-Enough
value. I'm using FormControl.hasError('ValidatorsName')
to get the errors with validation, but it seems like I cannot differentiate those patterns.
Template:
<mat-form-field class="form-field">
<input matInput placeholder="Logitude" [formControl]="longitude">
<mat-error *ngIf="longitude.hasError('pattern')">
Longitude must be <strong>a number</strong>
</mat-error>
<!-- I want to be able to check patter2 for example -->
<mat-error *ngIf="longitude.hasError('pattern')">
Longitude must have <strong>a minimum of 5 decimal numbers</strong>
</mat-error>
</mat-form-field>
And my Angular code:
this.longitude = new FormControl(this.attraction.longitude, [
// is valid number
Validators.pattern(new RegExp('(\d{1,3})([.|,]{,1})(\d+))','gi')),
// is precise enough
Validators.pattern(new RegExp('(\-{0,1})(\d{1,3})([.|,]{1})(\d{5,13})','i'))
]);
Is there any way to give those patterns an identifier? I'll appreciate any help.