Set minlength Validation in HTML5



Sometimes there is a requirement to fill in a minimum number of characters in a form. This ensures that the user inputs the correct data into the input field. In this article, we will see how to set the Minlength validation in HTML.

What is Minlength Validation?

In HTML the minlength is an attribute of the input tag of the form element. It is set in the input field to ensure that the users enter at least a minimum number of characters in the form input field.

For example, while setting a password for some websites, there are a few criteria that need to be met, such as a mixture of lowercase, and uppercase letters, special characters, and digits. There is one more criteria where you are required to enter at least some minimum number of characters. All this is done for security reasons.

Setting Minlength Validation in HTML

While entering the data if the minlength criteria is met and the submit button is clicked, the form will submit the data and perform the next operation. In case the minimum length criteria is not met, then while submitting the form, the browser will raise an error that the minimum length is not met by the user.

Syntax

<input type="text" minlength="8">

Example

Here is a code example for a better understanding of the minlength attribute.

<!DOCTYPE>
<html>
<body>
    <form action="/submit" method="post">
        <label>Username (Mix 5 characters):</label>
        <input type="text" minlength="5">
        <br>
        <button type="submit">Submit</button>
    </form>
</body>
</html>

Output

Tanya Sehgal
Tanya Sehgal

Python and HTML

Updated on: 2024-10-11T09:41:10+05:30

97 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements