Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
43
rated 0 times [  48] [ 5]  / answers: 1 / hits: 18355  / 11 Years ago, wed, june 26, 2013, 12:00:00

How can I set a max date (of the current date) using Jquery Validation?



$('#form').validate({
rules: {
reportDate: {
date: true
//Max date rule of current date...
}
}
});

More From » jquery

 Answers
18

You can add a validation method to set max date as current date using addMethod as this



$.validator.addMethod(maxDate, function(value, element) {
var curDate = new Date();
var inputDate = new Date(value);
if (inputDate < curDate)
return true;
return false;
}, Invalid Date!); // error message


and then add this method to the rules to set validation



$(#frm).validate({
rules: {
reportDate: {
required: true,
date: true,
maxDate: true
}
}
});


Note: the date '12/03/2013' is interpreted as this 'mm/dd/yyyy',

so 12/03/2013 > 06/26/2013 (today's date) and hence Invalid.



Demo


[#77411] Monday, June 24, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
deanna

Total Points: 84
Total Questions: 86
Total Answers: 107

Location: Cyprus
Member since Wed, Dec 8, 2021
3 Years ago
;