Skip to content

Instantly share code, notes, and snippets.

@bcswartz
Created November 23, 2017 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bcswartz/02e342a5a0de93df49baff8534cc03b8 to your computer and use it in GitHub Desktop.
Save bcswartz/02e342a5a0de93df49baff8534cc03b8 to your computer and use it in GitHub Desktop.
Example of a custom validation method for the jQuery Validation plugin to ensure one date is greater than the other
jQuery.validator.addMethod("dateComparison",function(value,element) {
var result= true;
if($("#startDateOptionLater:checked").length== 1)
{
var dateArray= $("#startDate").val().split("/");
var startDateObj= new Date(dateArray[2],(dateArray[0]-1),dateArray[1],0,0,0,0);
}
else
{
var exactDate= new Date();
var year= exactDate.getFullYear();
var month= exactDate.getMonth();
var day= exactDate.getDate();
var startDateObj= new Date(year,month,day,0,0,0,0);
}
var endDateArray= $("#endDate").val().split("/");
var endDateObj= new Date(endDateArray[2],(endDateArray[0]-1),endDateArray[1],0,0,0,0);
var startDateMilliseconds= startDateObj.getTime();
var endDateMilliseconds= endDateObj.getTime();
if (endDateMilliseconds <= startDateMilliseconds)
{
result= false;
}
return result;
},"The ending date must be a later date than the start date");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment