1

We need to show a input box to user to enter the expiry date. The expiry date will be usually mm/yyyy. Ex, 10/2018

Is there a way to accept only MM/YYYY format values in textbox?

I have not seen any date option with input type="date".

Month should be with in valid range --- 01 to 12 only

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
Billa
  • 5,226
  • 23
  • 61
  • 105

3 Answers3

3

If you can use jquery, take a look at Working fiddle, using Datepicker | jQuery UI.

HTML :

<label for="startDate">Date :</label>
<input name="startDate" id="startDate" class="date-picker" />

JS :

$(function() {
    $('#startDate').datepicker({
        dateFormat: "mm/yy"
    }) 
    .datepicker('setDate', new Date())
    .datepicker("option", "changeMonth", true)
    .datepicker("option", "changeYear", true)
    .datepicker("option", "showButtonPanel", true)
    .datepicker("option", "onClose", function(e){
         var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
         var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
         $(this).datepicker("setDate",new Date(year,month,1));
    })
});

Hope this helps.

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
2

http://eternicode.github.io/bootstrap-datepicker/?markup=input&format=&weekStart=&startDate=&endDate=&startView=0&minViewMode=0&todayBtn=false&clearBtn=false&language=en&orientation=auto&multidate=&multidateSeparator=&keyboardNavigation=on&forceParse=on#sandbox

This is THE best resource which I have found for datepicker. Solves almost EVERY need.

Use the following settings in the image below for the fields of

  1. Format 2.Start View, 3.Min View.

Based on your requirement of having MM-YYYY field. You may need to add custom validation to make sure you are accepting the requisite format ONLY.

Steps:

  1. Include the JS/CSS files in your project.

  2. After selecting and testing the kind of datepicker you want. Then copy the html/js code from the bottom right and include in your application.

enter image description here

Hope this helps!

CodePhobia
  • 1,315
  • 10
  • 19
1

You can use <input type="month">. The user can insert month and year. But it's not supported by IE and Firefox.

talkahe
  • 11
  • 3