minlength and maxlength are attributes in HTML that control the length of input box characters. Pay attention to the correct usage and common problems. For example: Setting minlength="5" maxlength="20" can limit input characters from 5 to 20. 1. The attributes are only applicable to text type input types; 2. It is required to use it with required to avoid null values passing verification; 3. The browser prompt information may vary from device to device; 4. Common problems include spelling errors, compatibility, and script modification content resulting in invalidation; 5. In actual applications, it is recommended to use real-time counting style prompts and always perform back-end verification, while avoiding too small minlength affecting the experience.
Form verification is a very common requirement in web development, especially when it involves user input. In HTML, minlength
and maxlength
are two very practical properties that control the number of characters allowed to be entered in text class input boxes such as <input>
or <textarea></textarea>
. If used properly, it can improve the user experience and reduce the pressure on back-end verification.

Basic usage: Set the input length range
These two properties usually appear together to limit the minimum and maximum length of content input by the user. For example:
<input type="text" minlength="5" maxlength="20">
The above code means that the user must enter at least 5 characters, and must not exceed 20 characters at most. If the user tries to submit a value that does not meet the criteria, the browser will automatically prompt an error.

What should be noted is:
- These two properties are only applicable to text class input types such as
type="text"
andtype="password"
. - They do not block user input, but are validated when submitted.
- The built-in prompt information in the browser may vary slightly depending on the system language or browser.
FAQ: Why does the settings not take effect?
Sometimes you will find that setting minlength
and maxlength
seems to have no effect, which is usually caused by the following reasons:

- Spelling error : For example, if written as
minLength
ormax-length
, the HTML attribute is case-sensitive and should be used in all lower case. - Not used with required : If you want the user to fill in and meet the length requirements, remember to add
required
, otherwise the empty value will also pass the verification. - Some browser compatibility issues : Although modern browsers basically support it, additional processing may be required on some older devices.
- JavaScript dynamically modified the value : Sometimes the front-end script clears or modifys the input box content, resulting in invalid verification.
For example, the following input box requires the user to enter an 8 to 16-digit password:
<input type="password" minlength="8" maxlength="16" required>
Only by combining it in this way can we truly play a verification role.
Practical application suggestions
In actual projects, there are several tips to let you use these two properties better:
- Match style tips : You can use JavaScript to listen to input events, display the remaining word count in real time when the user enters, or mark the red reminder.
- Note the length of Chinese characters : Chinese characters are also considered as a unit of character length, and there is no need to deal with encoding issues specifically.
- The backend also needs to verify : Although the frontend has verification, never rely on it as the only line of defense, and the backend should also do the same check.
- Avoid setting minlength that is too small : For example, setting it to 3 may cause users to feel that there are too many restrictions, which will affect the experience.
To give a simple example, you can implement a real-time counting function like this:
<textarea maxlength="100"></textarea> <div id="counter">100 words remaining</div> <script> const textarea = document.querySelector('textarea'); const counter = document.getElementById('counter'); textarea.addEventListener('input', () => { const remaining = 100 - textarea.value.length; counter.textContent = `${remaining} word remaining`; }); </script>
This way the user can clearly know how many more words they can enter.
Basically that's it. Although these two attributes seem simple, if used correctly, it can save a lot of trouble.
The above is the detailed content of HTML `minlength` and `maxlength` Attributes. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

The rational use of semantic tags in HTML can improve page structure clarity, accessibility and SEO effects. 1. Used for independent content blocks, such as blog posts or comments, it must be self-contained; 2. Used for classification related content, usually including titles, and is suitable for different modules of the page; 3. Used for auxiliary information related to the main content but not core, such as sidebar recommendations or author profiles. In actual development, labels should be combined and other, avoid excessive nesting, keep the structure simple, and verify the rationality of the structure through developer tools.

To use HTML button elements to achieve clickable buttons, you must first master its basic usage and common precautions. 1. Create buttons with tags and define behaviors through type attributes (such as button, submit, reset), which is submitted by default; 2. Add interactive functions through JavaScript, which can be written inline or bind event listeners through ID to improve maintenance; 3. Use CSS to customize styles, including background color, border, rounded corners and hover/active status effects to enhance user experience; 4. Pay attention to common problems: make sure that the disabled attribute is not enabled, JS events are correctly bound, layout occlusion, and use the help of developer tools to troubleshoot exceptions. Master this

Metadata in HTMLhead is crucial for SEO, social sharing, and browser behavior. 1. Set the page title and description, use and keep it concise and unique; 2. Add OpenGraph and Twitter card information to optimize social sharing effects, pay attention to the image size and use debugging tools to test; 3. Define the character set and viewport settings to ensure multi-language support is adapted to the mobile terminal; 4. Optional tags such as author copyright, robots control and canonical prevent duplicate content should also be configured reasonably.

TolearnHTMLin2025,chooseatutorialthatbalanceshands-onpracticewithmodernstandardsandintegratesCSSandJavaScriptbasics.1.Prioritizehands-onlearningwithstep-by-stepprojectslikebuildingapersonalprofileorbloglayout.2.EnsureitcoversmodernHTMLelementssuchas,

Using HTML sums allows for intuitive and semantic clarity to add caption text to images or media. 1. Used to wrap independent media content, such as pictures, videos or code blocks; 2. It is placed as its explanatory text, and can be located above or below the media; 3. They not only improve the clarity of the page structure, but also enhance accessibility and SEO effect; 4. When using it, you should pay attention to avoid abuse, and apply to content that needs to be emphasized and accompanied by description, rather than ordinary decorative pictures; 5. The alt attribute that cannot be ignored, which is different from figcaption; 6. The figcaption is flexible and can be placed at the top or bottom of the figure as needed. Using these two tags correctly helps to build semantic and easy to understand web content.

How to make HTML mail templates with good compatibility? First, you need to build a structure with tables to avoid using div flex or grid layout; secondly, all styles must be inlined and cannot rely on external CSS; then the picture should be added with alt description and use a public URL, and the buttons should be simulated with a table or td with background color; finally, you must test and adjust the details on multiple clients.

class, id, style, data-, and title are the most commonly used global attributes in HTML. class is used to specify one or more class names to facilitate style setting and JavaScript operations; id provides unique identifiers for elements, suitable for anchor jumps and JavaScript control; style allows for inline styles to be added, suitable for temporary debugging but not recommended for large-scale use; data-properties are used to store custom data, which is convenient for front-end and back-end interaction; title is used to add mouseover prompts, but its style and behavior are limited by the browser. Reasonable selection of these attributes can improve development efficiency and user experience.

When there is no backend server, HTML form submission can still be processed through front-end technology or third-party services. Specific methods include: 1. Use JavaScript to intercept form submissions to achieve input verification and user feedback, but the data will not be persisted; 2. Use third-party serverless form services such as Formspree to collect data and provide email notification and redirection functions; 3. Use localStorage to store temporary client data, which is suitable for saving user preferences or managing single-page application status, but is not suitable for long-term storage of sensitive information.
