In my template driven form the textarea is required based on a condtion. I would also like the pattern to be conditional. I tried this syntax based on a question from angular 1.x but it doesn't seem to work. - pattern=" condition ? '.*[^ ].*' : ''" I also tried - pattern="condition ? (\s*[^\s]+\s*)+ : .*". is this possible with the later versions of Angular? Here is my stack blitz: https://stackblitz.com/edit/conditionalpattern
Conditional pattern to be used along with required Angular7/8
2.4k Views Asked by Flash At
2
I think if you want to pass an expression, you should wrap pattern in square brackets. But more importantly, you cannot use the attribute
patternin the tagtextarea, becausepatternis not an allowed attribute fortextarea.As described in MDN Web Docs,
So in your case I would either use an
inputor consider using aCustomValidator. If you use an input, make sure to wrap the expression in brackets:Here's a tiny stackblitz with a working example of an input field.