I am new to angular 6 ,Here I want to validate a user input field and display different error messages based on the given input.
In my project I am using Angular Material design for UI.
What I want to do is
- If the user click on the save button display a error message as "Please enter your FName".
- If user touched but not enter anything and clicked on save button display a error message as "Please enter your FName".
- If user started to enter char it should show a error message as "minimum 4 char required"
- Once the user reached 15 char it should show a error message as "Maximum 20 char only"
- If the user enter any special char and space and other input an error message as "Enter only alphabets"
Now it changes to RED color when the input does not meet the successful validation.but I want to display an error message for each validation in the formControl.
Here I have a mat-input-field.
<form [formGroup]="userAddressValidations" novalidate>
<mat-form-field appearance="outline" class="col-sm-6">
<mat-label>First Name</mat-label>
<input matInput formControlName="firstName">
</mat-form-field>
</form>
<button mat-raised-button class="continueBtnHeight" color="warn">
<span>Save</span>
</button>
Ts file
export class ButtonToggleOverviewExample {
userAddressValidations: FormGroup;
constructor(private formBuilder: FormBuilder) { }
ngOnInit() {
this.userAddressValidations = this.formBuilder.group({
firstName: ['', [Validators.required, Validators.minLength(4), Validators.maxLength(20), Validators.pattern('[a-zA-Z]+')]]
});
}
}
stackblitz:https://stackblitz.com/edit/angular-t1k1x6-skowwq?file=app%2Fbutton-toggle-overview-example.ts
can anyone help me to solve this .