Skip to content
Related Articles

Related Articles

Improve Article

How to specify minimum & maximum number of characters allowed in an input field in HTML ?

  • Last Updated : 30 Jun, 2021

Given an input field and the task is to set the minimum/maximum number of characters allowed in an input field in HTML.

  • To set the maximum character limit in input field, we use <input> maxlength attribute. This attribute is used to specify the maximum number of characters enters into the <input> element.
  • To set the minimum character limit in input field, we use <input> minlength attribute. This attribute is used to specify the minimum number of characters enters into the <input> element.

Syntax: 

<input maxlength="number1">
<input minlength="number2"> 

Attribute Value: 

  • number1: It contains single value number which allows the maximum number of character in <input> element. Its default value is 524288.
  • number2: The minimum number of character required in input fields.

Example 1: Set the limit of maximum number of characters allowed in input fields.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <style>
        body {
            text-align: center;
        }
  
        h1 {
            color: green;
        }
    </style>
</head>
  
<body>
    <h1>GeeksforGeeks</h1>
    <h3>
        How to specify the maximum number
        of characters <br>allowed in an
        input field in HTML?
    </h3>
    <form action="#">
        Username:
        <input type="text" name="usrname" 
            maxlength="12"><br><br>
  
        Password:
        <input type="password" name="password"
            maxlength="10"><br><br>
              
        <input type="submit" value="Submit">
    </form>
</body>
  
</html>

 



Output: 

Example 2: Set the limit of minimum number of characters allowed in input fields.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <style>
        body {
            text-align: center;
        }
  
        h1 {
            color: green;
        }
    </style>
</head>
  
<body>
    <h1>GeeksforGeeks</h1>
    <h3>
        How to specify the minimum number
        of characters <br>allowed in an
        input field in HTML?
    </h3>
    <form action="#">
        Username:
        <input type="text" name="usrname" 
            minlength="12"><br><br>
  
        Password:
        <input type="password" name="password" 
            minlength="10"><br><br>
              
        <input type="submit" value="Submit">
    </form>
</body>
  
</html>

Output:

Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML  course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :