i saw this example : Validate that end date is greater than start date with jQuery
talking about the validation of end date greater than start, date format for my input are always mm/yyyy, so it’s not working for me if :
start date > 122000
end date > 012002
I think i have to split my date, to just check on yyyy, if is greater or not, but didn’t know how to resolve it … here is my code ( i want always to say that if i have not 7 characters in my input to don’t pass form submit :
HTML
<input type="text" id="starddate" />
<input type="text" id="enddate" />
<div id="msg"></div>
<input type="submit" id="submit" />
JS
jQuery.validator.addMethod("greaterThan",
function(value, element, params) {
if (!/Invalid|NaN/.test(new Date(value))) {
return new Date(value) > new Date($(params).val());
}
return isNaN(value) && isNaN($(params).val())
|| (parseFloat(value) > parseFloat($(params).val()));
},'Must be greater than {0}.');
$("#enddate").rules('add', { greaterThan: "#startdate" });
If your date values are strings are in the form
mm/yyyy, then you can’t usenew Date(value)to create aDateobject for them — that’s not a format that theDateconstructor is documented as accepting (and until recently, it wasn’t documented to accept anything at all except whatever the output oftoStringwas, which was also up to the implementation to decide).But the problem’s not difficult: Given dateA and dateB which are strings in that format:
or less clearly
E.g., you can just compare strings if you move the year before the month, because the string
"200012"is<the string"200201".