Home  >  Article  >  Web Front-end  >  How to use month input type in HTML?

How to use month input type in HTML?

PHPz
PHPzforward
2023-08-20 20:21:16903browse

In HTML, a form of various elements which helps in making a user interface in a web page. Using which we can collect different nature of nature.

One of the commonly used control is Month control i.e.

This control basically provides the user with a calendar-like drop-down menu from which the user can select or select the month and year. Month The control allows you to select a date in the format YYYY-MM, where YYYY represents the year and MM represents the month.

Let's look at a simple example using the Month control.

Example

<html>
<body>
   <form name="form1">
      <label for="adm">Date of Admission:</label>
      <input type="month" name="doa">
   </form>
</body>
</html>

Executing the code given above, a month control will be displayed on the page.

When you click on the calendar icon on the right side of the control, it will open the complete month calendar like this −

How to use month input type in HTML?

Once the drop-down menu opens, you can select the month and year from the calendar, or you can enter the month and year in the control.

Once the month and year is selected, it will get stored in String type of value.

Let us create a program to display the selected month and year from the control using JavaScript.

Example

<html>
<head>
   <title>Admission Form</title>
   <script>
      function display(){
         d=form1.doa.value;
         document.write("<center><b>Date of Admission is "+d 
         +"</b></center>");
      }
   </script>
   <form name="form1">
 	   <label for="adm">Date of Admission:</label>
	   <input type="month" name="doa" onchange="display()">
   </form>
<html>

In this program, we have used <script> tag to write JavaScript code in which a function is created to display the selected month when onchange() event is triggered. onchange event will be fired when a date value is selected from the control. document.write() will display the value on the next page. We have displayed the date of admission in the formatted output by using <HTML> formatting tags.</script>

It is displaying date value in YYYY-MM format.

We can also display the output when the submit button is clicked. In this case, we will use the submit button's onclick() event instead of onchange().

Example

<html>
<head>
   <title>Admission Form</title>
   <script>
      function display(){
         d=form1.doa.value;
         document.write("<center><b>Date of Admission is "+d
         +"</b></center>");
      }
   </script>
   <form name="form1">
      <label for="adm">Date of Admission:</label>
      <input type="month" name="doa">
      <br>
      <input type="submit" value="Submit" onclick="display()">
   </form>
<html>

We can also set the default value of month and year which should be displayed when the web page gets loaded in the browser. To do so, we can use value attribute in tag.

Example

<html>
<body>
   <form>
      <p>
         Select a month: <input type="month" name="selectedmonth"  value="1980-12">
         <br>
         <input type="submit" value="Send data">
      </p>
   </form>
</body>
</html>

Once this program is executed, it will show “December, 1980” as the default value in the month control.

Next comes the very interesting part, which is setting the date range, which means the user can only enter date values ​​within the range, otherwise the program will display an error message.

Example

<html>
<body>
   <form>
      <p>
         Select a month: <input type="month" name="selectedmonth"       value="1980-12" min="1980-12" max="1981-11">
         <br>
         <input type="submit" value="Send data">
      </p>
   </form>
</body>
</html>

In this program, you can observe that only December month (year 1980) is active and others are disabled and similarly when you type the year 1981, it will show you months upto November because we have set the range.

If user types the invalid month or year name, it will show an error message like this −

How to use month input type in HTML?

We can also make use of step attribute using which we can skip few months for the given year while setting the date range. Look at the example shown below of an event registration form where assuming that a particular event is falling under specific months.

Example

<html>
<body>
   <form name="form1">
      <table>
      <tr>
         <td><label for="adm">Choose Event :</label></td>
         <td><select name="event">
            <option>Live Concert
            <option>Olympics in Beijing
            <option>World Series(Baseball)
            </select>
         </td>
      </tr>
      <tr>
         <td>
         <label for="adm">Available Event Dates:</label></td>
         <td><input type="month" name="eventdate" step="3"></td>
      </tr>
      <tr>
         <td></td><td><input type="submit" value="Submit"></td>
      </tr>
   </form>
</body>
</html>

The above is the detailed content of How to use month input type in HTML?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete
Previous article:Image buttons using HTML5Next article:Image buttons using HTML5