Using min and max attributes in HTML5
The min and max attributes are used with number and date input types to provide restriction for input. min attribute specifies a minimum value and max attributes specify a maximum value for the input field. Now we use these attributes into form. We write the following code:
- <!DOCTYPE HTML>
- <html>
- <body>
- <form method="post">
- No.
- <input type="number" name="num" min="1" max="10" />
- <input type="submit" />
- </form>
- </body>
- </html>
Now we run this code. The output will look like below:
We note here, textbox accepts only values between 1 and 10. If we enter other
value and click on the submit button, then the message is displayed as below
figure:
We can set the default value to
it by writing the following code:
- <input type="number" name="num" min="1" max="10" value="2"/>
Now we run this code. The output will look like below:
We use these attributes with date input type. We write the following code:
- <!DOCTYPE HTML>
- <html>
- <body>
- <form method="post">
- Date
- <input type="date" name="dd" min="2000-01-01" max="2020-01-01" />
- <input type="submit" />
- </form>
- </body>
- </html>
The output will look like
below:
This will accept the date only between 2000-01-01 and 2020-01-01. If we enter other date and click on submit
button then the message is displayed as below: