An 18-year validation rule for the jQuery Validator plugin using addMethod .
jQuery.validator.addMethod( "validDOB", function(value, element) { var from = value.split(" "); // DD MM YYYY // var from = value.split("/"); // DD/MM/YYYY var day = from[0]; var month = from[1]; var year = from[2]; var age = 18; var mydate = new Date(); mydate.setFullYear(year, month-1, day); var currdate = new Date(); var setDate = new Date(); setDate.setFullYear(mydate.getFullYear() + age, month-1, day); if ((currdate - setDate) > 0){ return true; }else{ return false; } }, "Sorry, you must be 18 years of age to apply" );
and
$('#myForm') .validate({ rules : { myDOB : { validDOB : true } } });
source share