Error if age is under 21
-
How can I trigger an error on submit if the Date of Birth (date picker) is under 21. I have the code below but not sure where to put it. Any help is highly appreciated:
I have the very best luck with stackoverflow with all my learning and I really appreciate it.
I have a contact form 7 wp plugin and datepicker plug install and working. I would like to see if somebody can help me with my question or lead me to the right place.
I would like to present an error message to user who are not 18 years of age. They can not move forward with submitting contact form. If somebody is familiar with this, please help.
Thanks,
I found this code, but not sure where to put it in my plugin:
$(document).ready(function () {
$(“#DOB”).datepicker({
changeMonth: true,
changeYear: true,
dateFormat: ‘yy-mm-dd’,onSelect: function (date) {
var dob = $(‘#DOB’).datepicker(‘getDate’);
var d2 = new Date();
var diff = 0;
if (dob && d2) {
diff = Math.floor((d2.getTime() – dob.getTime()) / 86400000); // ms per day
}
diff = Math.floor(diff / 365);
console.log(diff);if (diff < 18) {
//OUTPUT ERROR
} else {
//DO NOTHING, MOVE FORWARD.
}
}
});});
- The topic ‘Error if age is under 21’ is closed to new replies.