Lightning input of type text in LWC accepts blank space as input when set to required - how to get rid of it?
My Lightning input of type text in lightning web components is accepting blank space as input, when it is set to required. How to get rid of it?
Lightning Input has a pattern attribute that lets you specify a regular expression that the input's value must follow. Every value that doesn't match with such a pattern will mark the component as invalid and will show an error. You can customize the error message setting message-when-pattern-mismatch attribute.
This pattern will allow only strings that begin and end with a non-whitespace character: ^S.*S$
^ means the beginning of a line
S means a non-whitespace character
. means any character
* is a quantifier and means zero or more times, so .* means any character can appear zero or more times
$ means the end of a line
You can find pattern rules and special characters here.