How to Set Character Limits in HTML Input Fields? Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report It's often necessary to restrict the number of characters a user can input. This ensures data consistency and prevents users from entering values that are too long or too short. In this article, we will learn how to set the minimum and maximum number of characters allowed in an HTML input field.To set the minimum character limit in the input field, we use <input> minlength attribute. This attribute is used to specify the minimum number of characters entered into the <input> element.Syntax: <input maxlength="number1"> <input minlength="number2"> How to Set Character Limits in HTML Input FieldsHTML provides built-in attributes for specifying the minimum and maximum character limits for input fields:maxlength Attribute: Sets the maximum number of characters allowed.minlength Attribute: Sets the minimum number of characters required.Example 1: Setting a Maximum Character LimitIn this example, we will create an input field with a maximum character limit of 12. 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="username" 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: Setting Both Minimum and Maximum Character LimitsIn this example, we will set a minimum character limit of 12 and a maximum character limit of 20 for an input field. 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 & minimum number of characters <br>allowed in an input field in HTML? </h3> <form action="#"> Username: <input type="text" name="username" minlength="12" maxlength="20"><br><br> Password: <input type="password" name="password" minlength="10" maxlength="20"> <br><br> <input type="submit" value="Submit"> </form> </body> </html> Output: How to specify minimum &maximum number of characters allowed in an input field in HTML ? Comment More infoAdvertise with us Next Article How to define keyboard input in HTML5 ? V vkash8574 Follow Improve Article Tags : Web Technologies HTML HTML-Attributes HTML-Questions Similar Reads How to restrict user character input limit in ReactJS ? To restrict the user character input limit in React JS we can simply set a max input limit to the input box or we can also monitor the input length and make a custom function to set the restriction. Pre-requisite:NPM & Node.jsReact.jsReact HooksHTML onchange EventApproaches to restrict user char 3 min read How to create a multiline input control text area in HTML5 ? The HTML <textarea> tag is used to specify a multiline input control text area in HTML5. Â The <cols> and <rows> attributes specify size of a textarea. Syntax <textarea rows="" cols=""> Contents... </textarea> The <textarea> tag contains 5 attributes that are liste 1 min read How to set float input type in HTML5 ? HTML5 is the latest version of Hypertext Markup Language (HTML), the standard language for creating web pages and web applications. With HTML5, new input types were introduced to make web form creation easier and more accessible. These input types include text, password, checkbox, radio, submit, res 3 min read How to define keyboard input in HTML5 ? The phrase tag is used to define the keyboard input. The text enclosed by <kbd> tag is typically displayed in the browserâs default monospace font. In this article, we define the keyboard input by using various types of tags like <kbd>, <code>, <em>, <var>, and <samp 1 min read How to define keyboard input in HTML5 ? The phrase tag is used to define the keyboard input. The text enclosed by <kbd> tag is typically displayed in the browserâs default monospace font. In this article, we define the keyboard input by using various types of tags like <kbd>, <code>, <em>, <var>, and <samp 1 min read How to define keyboard input in HTML5 ? The phrase tag is used to define the keyboard input. The text enclosed by <kbd> tag is typically displayed in the browserâs default monospace font. In this article, we define the keyboard input by using various types of tags like <kbd>, <code>, <em>, <var>, and <samp 1 min read Like