Explore Courses Blog Tutorials Interview Questions
0 votes
in Web Technology by (20.3k points)

It seems the minlength attribute for an <input> field doesn't work.

Is there any other attribute in HTML5 with the help of which I can set the minimal length of value for fields?

1 Answer

0 votes
by (40.7k points)

You can try using the pattern attribute like this:

<input pattern=".{3,}"   required title="3 characters minimum">

<input pattern=".{5,10}" required title="5 to 10 characters">

But, If you want to create the option to use the pattern for "empty, or minimum length", then you can do as follows:

<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)">

Note: The required attribute is also needed, otherwise an input field with an empty value will be excluded from constraint validation.

Browse Categories