Pages

11 Sept 2013

Validate Date of Birth is not greater than current date using jQuery

jQuery, jQuery DatePicker, jQuery UI, jQuery UI DatePicker

This is a common situation where Date of birth needs to validated against the current or today's date. I have to implement the same functionality for my project. I have used jQuery UI datepicker control to select date and textbox was read only, it was pretty easy to implement.

All I needed to do is to stop datepicker control to disable dates greater than today's date. jQuery UI datepicker control provides an option to set the max date. If its value is set to "-1d" then all the dates after current date will be disabled.

Note: I have set the year range statically but this can be set programmatically.

$(document).ready(function(){
  $("#txtDate").datepicker(
  {
  yearRange:
  '<%=(System.DateTime.Now.Year - 150).ToString()%>:
  <%=System.DateTime.Now.Year.ToString() %>',
  changeMonth:true,
  changeYear:true,

  maxDate: '-1d'
  });
});

No comments:

Post a Comment