The following code will allow a minimum of 2 characters and a maximum of 20 characters and not allow blank:
<input type="text" name="nome" placeholder="Nome" pattern="^[a-zA-Z][a-zA-Z0-9-_\.]{1,20}$" required>
If you do not want a number (only letters):
<input type="text" name="nome" placeholder="Nome" pattern="^[a-zA-Z][a-zA-Z-_\.]{1,20}$" required>
Allow at least 4 characters:
<input type="text" name="nome" placeholder="Nome" pattern="^[a-zA-Z][a-zA-Z-_\.]{3,20}$" required>
See some explanations:
-
^
- Asserts position at the beginning of the string.
-
$
- Asserts position at the end of the string, or before the right line terminator at the end of the string (if any).
-
-_
- corresponds to a single character in the -_ (case-sensitive) list.
-
{1,20}
- Quantifier - Matches between 1 and 20 times as many times as possible, returning as needed.
-
a-z
- a single character in the range between one (ASCII 97) and z (ASCII 122) (case sensitive).
-
A-Z
- a single character in the range between A (ASCII 65) and Z (ASCII 90) (case sensitive).
See more examples here: link .
jsfiddle: link
regex101: link