Is there a minlength validation attribute in HTML5?
Yes, there it is. It's like maxlength. W3.org documentation: http://www.w3.org/TR/html5/forms.html#attr-fe-minlength
In case minlength
doesn't work, use the pattern
attribute as mentioned by @Pumbaa80 for the input
tag.
For textarea:
For setting max; use maxlength
and for min go to this link.
You will find here both for max and min.
You can use the pattern
attribute. The required
attribute is also needed, otherwise an input field with an empty value will be excluded from constraint validation.
<input pattern=".{3,}" required title="3 characters minimum">
<input pattern=".{5,10}" required title="5 to 10 characters">
If you want to create the option to use the pattern for "empty, or minimum length", you could do the following:
<input pattern=".{0}|.{5,10}" required title="Either 0 OR (5 to 10 chars)">
<input pattern=".{0}|.{8,}" required title="Either 0 OR (8 chars minimum)">
Here is HTML5-only solution (if you want minlength 5, maxlength 10 character validation)
http://jsfiddle.net/xhqsB/102/
<form>
<input pattern=".{5,10}">
<input type="submit" value="Check"></input>
</form>
There is a minlength
property in the HTML5 specification now, as well as the validity.tooShort
interface.
Both are now enabled in recent versions of all modern browsers. For details, see https://caniuse.com/#search=minlength.