I am using mat form field in my angular form and my code for one of my attribute is as follows.
<mat-form-field appearance="fill">
<mat-label>CountryCode</mat-label>
<input formControlName="CountryCode" matInput (click)='updateFormTextValue("alphanumericNext", "CountryCode")'>
</mat-error>
</mat-form-field>
I want to add validation for this field such that if the country is Canada, I want to do Validators.required and Validators.maxLength(30) and if country is other than Canada I just need to check the max length. How can I do that?
My code in service class is as follows
CountryCode: new FormControl('', [Validators.required, Validators.maxLength(30)]),
I am able to use validators provided by angular as my code above but don't know how to put validation as per conditions. I saw some examples on StackOverflow but didn't get how to do it.
PS: country for which I need to put check is one of the fields in my form and its code is same as CountryCode as mentioned above. Please help
Also, need to show the message that if the max length exceeds then message "maximum length exceeded should be shown" and if the field is empty and if the country is Canada then the message "field is required is shown"