Min, Max, Minlength, and Maxlength
What you'll learn: How to set numeric ranges and text length limits that browsers automatically enforce.
What Are These Attributes?
HTML gives you built-in validation tools that work before your form even reaches the server. The min, max, minlength, and maxlength attributes act like bouncers at a club—they check if input meets your rules and refuse entry if it doesn't.
Numeric Constraints: min and max
Use min and max with number, range, and date input types to set acceptable boundaries:
<label for="age">Age (18-120):</label>
<input type="number" id="age" name="age" min="18" max="120">
<label for="volume">Volume:</label>
<input type="range" id="volume" name="volume" min="0" max="100">
<label for="appointment">Appointment Date:</label>
<input type="date" id="appointment" name="appointment"
min="2024-01-01" max="2024-12-31">
If a user tries to submit 17 for age or picks a date outside your range, the browser blocks submission and shows an error message.
Text Length Constraints: minlength and maxlength
Use minlength and maxlength with text, email, password, tel, url, and textarea to control character counts:
<label for="username">Username (3-20 characters):</label>
<input type="text" id="username" name="username"
minlength="3" maxlength="20">
<label for="tweet">Tweet (max 280 characters):</label>
<textarea id="tweet" name="tweet" maxlength="280"></textarea>
The browser won't allow submission if text is too short or too long (though maxlength physically prevents typing beyond the limit).
Think of It Like This
These attributes are like filling out a paper form where someone's standing over your shoulder saying "That number's too high" or "Write at least 8 characters"—except the browser does it automatically.
Key Takeaway: Use min/max for numeric ranges and dates, minlength/maxlength for text lengths—the browser validates them automatically before form submission.