ngOnInit(): void {
this.customerForm = this.fb.group({
name: ['', Validators.required],
customer_id: ['', Validators.required],
})
}
I have this form group in angular. So This a form which contains name and customer_id. What I need is that I want to validate the name field.It shouldn't accept any special characters. If possible please mention also to mention in using toastr
<td>
<mat-form-field>
<mat-label>Name</mat-label>
<input matInput placeholder="Name" formControlName="name" autocomplete="off" required>
</mat-form-field>
</td>
You can create a custom validator to add to your form "name" so everytime the check fails the form will return an invalid state so you can show a message somewhere
Now add the CustomValidators to the component constructor:
It's then possible to show a mat-error to the user on the template just under the mat-form-field
You can do something similar with the Validators.required built-in.
For more informations about form validation check this