I am just starting out with Angular, so please bear with me if my question seems basic. I'm eager to learn and hopeful that I can find the answers I need here.
Currently, I have implemented validation for an input field that captures a user's First Name, and it is functioning correctly.
I am wondering why all these validations and error messages are in my HTML file.
If possible, could someone guide me on how to move this logic to the TypeScript file? Or perhaps correct any misunderstandings I may have.
Here is the code snippet:
<mat-form-field appearance="outline" class="col-sm-6">
<mat-label>First Name</mat-label>
<input matInput minlength="4" pattern="^[a-z]*$" maxlength="15" [formControl]="fName" required>
<mat-error *ngIf="fName.errors?.required">Please fill out this field</mat-error>
<mat-error *ngIf="fName.errors&&fName.errors?.minlength">Required minimum 4 characters</mat-error>
<mat-error *ngIf="fName.errors&&fName.errors.pattern">Enter only alphabets</mat-error>
</mat-form-field>
TypeScript:
fName = new FormControl();
The current setup works, but I am curious to know if achieving the same functionality from the TypeScript file is possible.
Apologies if my question is taking up your precious time.