HTML input field non-white spacing validation

In certain situations, there comes some validation to disallow white spaces in input filled. The below-given regex can use for such validation.

 <input type="text" pattern="[A-Za-z0-9]+" required> 

The above-given Regex shows invalid if, there exists any white space in the input field value.

If you want to validate the non-whitespace only for the starting and ending position of the input value, you can use the next given pattern for the input type.

 <input type="text" pattern="\S(.*\S)?" required> 

Leave a comment

Your email address will not be published. Required fields are marked *