Confirm date for someone over 18 using jQuery

I have a form on my website that should be validated for anyone 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 works in exactly the opposite way. I would like the if statement to take action when a user is over 18 years old.

Thanks for your help in advance.

+6
source share
6 answers

check out demo

  var day = 12; var month = 12; var year = 1996; 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){ // you are above 18 alert("above 18"); }else{ alert("below 18"); } 
+7
source
 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); if(currdate < mydate) { alert('You must be at least 18 years of age.'); } 
+3
source

Here is a slightly lighter version that I tested :

 var day = 1; var month = 1; var year = 1999; var age = 18; var cutOffDate = new Date(year + age, month, day); if (cutOffDate > Date.now()) { $('output').val("Get Outta Here!"); } else { $('output').val("Works for me!"); } 

The key is to add a minimum age to the date of birth and confirm that it is before the current date. You check if the current date minus the minimum age (basically the last date of birth) was greater than the date of birth, which will give you the other side.

+2
source

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 } } }); 
+1
source

if it works the other way around, did you try replacing > with < on the second to the last line?

0
source

I think it will be easier to see if the variables are renamed.

mydate => givenDate
currdate => thresholdDate

if givenDate> thresholdDate => you are not 18
else => you are 18

i.e.

 if ( givenDate > thresholdDate ){ // you are not 18 } 

i.e

 if ((givenDate - thresholdDate) > 0){ // you are not 18 } 

i.e.

 if ((mydate - currdate ) > 0){ // you are not 18 } 
0
source

Source: https://habr.com/ru/post/954406/


All Articles