Created
November 23, 2017 20:23
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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