I am using the Validators.pattern to check that the name
filed in my form contains only letters. The pattern I have added is lastName:[null,[Validators.required,Validators.pattern('[A-Za-z]+')]]
. But I notice that in case there is an error, the error message contains extra ^
and $
. Why? The code example is at https://stackblitz.com/edit/angular-xesgxe
Add an invalid name (say 1
) in Last Name
filed (not First Name
as I am using a different validator for it. You'll see the error The required pattern is: ^[A-Za-z]+$
. Notice the extra ^
and $
in the error string while the pattern was [A-Za-z]+
Check createForm()
function in signup-component.component.ts
. Look for line lastName:[null,[Validators.required,Validators.pattern('[A-Za-z]+')]]
The error message comes from ShowErrorsComponent
. Check 'pattern': (params) => 'The required pattern is: ' + params.requiredPattern
in errorMessages
in the class ShowErrorsComponent
.