Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate date for anyone over 18 with jQuery

I have a form on my site that should validate for anyone who is over 18.

var day = $("#dobDay").val();
var month = $("#dobMonth").val();
var year = $("#dobYear").val();
var age = 18;
var mydate = new Date();
mydate.setFullYear(year, month-1, day);

var currdate = new Date();
currdate.setFullYear(currdate.getFullYear() - age);
var output = currdate - mydate
if ((currdate - mydate) > 0){
    // you are not 18
}

But it working totally opposite way. I would like the if statement to take action when user is over under 18 years old.

Thank you for your help in advance

like image 264
Dom Avatar asked Sep 23 '13 10:09

Dom


People also ask

How to validate age above 18 in jQuery?

setFullYear(currdate. getFullYear() - age); if ((currdate - mydate) & lt; 0) { alert("Sorry, only persons over the age of " + age + " may enter this site"); return false; } return true; });

How to validate Date of birth in jQuery?

When a Date is selected, the onSelect event handler captures the Date value in String format and the JavaScript object of the HTML element i.e. the TextBox to which the jQuery DatePicker plugin is applied and the Date value is passed to the ValidateDOB JavaScript function.

How to validate Date in jQuery DatePicker?

This can be done by adding changeMonth (similarly, changeYear) to the datepicker's options: { minDate: “-3M -15D”, maxDate: “+3M +15D”,changeMonth:true,changeYear:true }.

How do you validate your age?

1. Date format dd/MM/yyyy validation: The Date of Birth (DOB) will be first validated for dd/MM/yyyy format using Regular Expression (Regex). 2. 18+ Minimum Age validation: The difference between the age entered in the TextBox and the Current Date is minimum 18 years.


1 Answers

check this DEMO

var day = 12;
var month = 12;
var year = 2006;
var age = 18;
var setDate = new Date(year + age, month - 1, day);
var currdate = new Date();

if (currdate >= setDate) {
  // you are above 18
  alert("above 18");
} else {
  alert("below 18");
}
like image 135
developerCK Avatar answered Oct 18 '22 05:10

developerCK