Date validation for “MM/YYYY” using jQuery

You may done validation for "dd/mm/yyyy" format but today I need to do validation for "MM/YYYY" format. The end user will input the date in textbox and I need to validate the date. Below jQuery code validates the date using regular expression in "MM/YYYY" format only using jQuery.

jQuery Code:

$(document).ready(function() {
  $('#txtDate').blur(function() {
      if(validateDate('txtDate'))
      {
          alert('Date is valid');
      }
      else
      {
          alert('Invalid Date!!!');
      }
   });
});

//Functions Starts
function validateDate(txtDate){
   var txtVal = document.getElementById(txtDate).value;
   var filter = new RegExp("(0[123456789]|10|11|12)([/])([1-2][0-9][0-9][0-9])");
   if(filter.test(txtVal))
      return true;
   else
     return false;
}?
//Functions Ends
//

The above code validates the date for year 1000-2999. Don't forget to set the maxlength of text box to 7.

See result below:

(Note: You can also see the jQuery code and HTML code by clicking on the buttons given below.)

Also read "Validate Date using jQuery"

Feel free to contact me for any help related to jQuery, I will gladly help you.



Responsive Menu
Add more content here...