Java Script, Tutorials, Web

General Tips: How to validate from and to date of jquery datepicker control


I am using Jquery Date picker control and my page has from date and to date. This control already have some generic validations.

Here I am going to one scenarion where it is failing:

Validation statement: From date cannot be greater than to date.

If you select dates from calendar which is popup on focus or click of image, then it is not allowing invalid date. But if you typed in from date to make it greater than todate, it doesn’t validate.

You can validate dates like below:
Declaration of Date picker control


//Date picker
var dates = $("#from, #to").datepicker({
changeMonth: true,
changeYear: true,
numberOfMonths: 1,
dateFormat: "dd-M-yy",
onSelect: function (selectedDate) {
var option = this.id == "from" ? "minDate" : "maxDate",
instance = $(this).data("datepicker"),
date = $.datepicker.parseDate(
instance.settings.dateFormat, //||
// $.datepicker._defaults.dateFormat,
selectedDate, instance.settings);
dates.not(this).datepicker("option", option, date);
}
});

Validation Code (Comparison of dates)


if($('#from').datepicker("getDate")>$('#to').datepicker("getDate")) {
alert("From Date cannot be more than to date.");
return;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s