How to specify a minimum value in HTML?

The min attribute in HTML is used to specify the minimum allowed value for input elements. This attribute is primarily used with input types like number, range, date, datetime-local, month, time, and week to enforce validation constraints on user input.

Syntax

Following is the syntax for using the min attribute −

<input type="inputType" min="value" name="fieldName">

Where value represents the minimum acceptable value based on the input type. For numeric inputs, it's a number; for date inputs, it's a date string in the appropriate format.

Using Min with Number Input

The most common use of the min attribute is with type="number" to restrict numeric input to a specific range.

Example

Following example demonstrates using the min attribute with a number input field −

<!DOCTYPE html>
<html>
<head>
   <title>Min Attribute with Number Input</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <h2>Product Rating Form</h2>
   <form action="/submit" method="post">
      <label for="rating">Rate this product (1-10):</label><br>
      <input type="number" id="rating" name="rating" min="1" max="10" required><br><br>
      
      <label for="age">Your age (must be 18 or older):</label><br>
      <input type="number" id="age" name="age" min="18" max="120"><br><br>
      
      <input type="submit" value="Submit Rating">
   </form>
</body>
</html>

In this form, the rating input accepts values from 1 to 10, and the age input requires a minimum value of 18. If a user tries to enter a value below the minimum, the browser will show a validation error.

Product Rating Form
Rate this product (1-10): [input field]
Your age (must be 18 or older): [input field]
[Submit Rating button]

Using Min with Range Input

The min attribute also works with range sliders to set the minimum selectable value.

Example

<!DOCTYPE html>
<html>
<head>
   <title>Min Attribute with Range Input</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <h2>Volume Control</h2>
   <form>
      <label for="volume">Set Volume (10-100):</label><br>
      <input type="range" id="volume" name="volume" min="10" max="100" value="50">
      <span id="volumeValue">50</span><br><br>
      
      <label for="brightness">Brightness (0-100):</label><br>
      <input type="range" id="brightness" name="brightness" min="0" max="100" value="75">
      <span id="brightnessValue">75</span>
   </form>
   
   <script>
      document.getElementById('volume').addEventListener('input', function() {
         document.getElementById('volumeValue').textContent = this.value;
      });
      document.getElementById('brightness').addEventListener('input', function() {
         document.getElementById('brightnessValue').textContent = this.value;
      });
   </script>
</body>
</html>

The volume slider starts at 10 (minimum) and can go up to 100, while the brightness slider starts at 0. The current values are displayed and update as the user moves the sliders.

Volume Control
Set Volume (10-100): [slider] 50
Brightness (0-100): [slider] 75

Using Min with Date Inputs

The min attribute can specify the earliest selectable date for date-related input types.

Example

<!DOCTYPE html>
<html>
<head>
   <title>Min Attribute with Date Input</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <h2>Event Booking Form</h2>
   <form>
      <label for="eventDate">Select event date (from tomorrow onwards):</label><br>
      <input type="date" id="eventDate" name="eventDate" min="2024-01-02"><br><br>
      
      <label for="appointmentTime">Appointment time (9 AM - 6 PM):</label><br>
      <input type="time" id="appointmentTime" name="appointmentTime" min="09:00" max="18:00"><br><br>
      
      <input type="submit" value="Book Event">
   </form>
</body>
</html>

The date input prevents users from selecting dates before January 2, 2024, while the time input restricts selection to business hours between 9 AM and 6 PM.

Event Booking Form
Select event date (from tomorrow onwards): [date picker]
Appointment time (9 AM - 6 PM): [time picker]
[Book Event button]

JavaScript Validation with Min Attribute

You can also validate the minimum value using JavaScript for custom error handling.

Example

<!DOCTYPE html>
<html>
<head>
   <title>Min Validation with JavaScript</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <h2>Order Quantity</h2>
   <form id="orderForm">
      <label for="quantity">Quantity (minimum 5 pieces):</label><br>
      <input type="number" id="quantity" name="quantity" min="5"><br><br>
      <p id="errorMessage" style="color: red; display: none;">Minimum order quantity is 5 pieces</p>
      <button type="submit">Place Order</button>
   </form>
   
   <script>
      document.getElementById('orderForm').addEventListener('submit', function(e) {
         var quantity = parseInt(document.getElementById('quantity').value);
         var errorMsg = document.getElementById('errorMessage');
         
         if (quantity < 5) {
            e.preventDefault();
            errorMsg.style.display = 'block';
         } else {
            errorMsg.style.display = 'none';
            alert('Order placed for ' + quantity + ' pieces');
         }
      });
   </script>
</body>
</html>

This example shows custom validation that displays an error message if the user enters a quantity below the minimum value of 5.

Order Quantity
Quantity (minimum 5 pieces): [input field]
[Place Order button]
(Error message appears if quantity is less than 5)

Supported Input Types

The min attribute is supported by the following HTML input types −

Input Type Min Value Format Example
number Numeric value min="0"
range Numeric value min="1"
date YYYY-MM-DD format min="2024-01-01"
datetime-local YYYY-MM-DDTHH:MM format min="2024-01-01T09:00"
month YYYY-MM format min="2024-01"
time HH:MM format min="09:00"
week YYYY-W## format min="2024-W01"

Conclusion

The min attribute provides client-side validation to ensure users enter values that meet minimum requirements. It works with various input types including numbers, dates, and ranges, helping create more robust and user-friendly forms. Always combine client-side validation with server-side validation for complete security.

Updated on: 2026-03-16T21:38:53+05:30

204 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements