I have three fields in my form: status, ask date, and expected date
If my Status=A, I would like my expected date to be required in validation additionally I would like my expected date validated to always be greater than the ask date
so here are my rules
rules: {
status: {
required: true,
},
ask_date: {
required: true,
date:true,
},
expected_date: {
required: function(element){
if($('#status').val()=='A'){
return true;
} else {
return false;
}
},
greaterThan: "#ask_date",
date:true,
},
How can I say invoke the greaterThan method ONLY when the field meets the conditions for being required?
the "greaterThan" is custom jquery validation method i am using for this from a separate stack overflow here Validate that end date is greater than start date with jQuery
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())
|| (Number(value) > Number($(params).val()));
},'Must be greater than {0}.');