JavaScript Date of Birth Validation

user3749063 picture user3749063 · Jun 17, 2014 · Viewed 7.9k times · Source

It's working now. Thanks very much! EDIT I am using Dreamweaver and working on HTML, I think I chose C# by mistake on the post.

I am trying to add a validation that outputs an error when trying to submit if the D.O.B is over 18 years old. I can't get it to work along with the rest of the validations.

<form action="http://posttestserver.com/post.php" method="post">
<!--action="contact.php" name="form1" id="form1">
 -->
 <!-- stackoverflow.com, w3schools.com-->
First Name <input type="text" name="firstName" value="First Name" required/>
<br>
Last Name <input type="text" name="lastName" value="Last Name" required/>
<br>
Date of Birth <input type="date" name="dob" required/>
<br>
Address <input type="text" name="address" required/>
<br>
Mobile Number <input type="number" name="mobile" required/>
<br>
  Email Address  <input name="email" type="email" value="@." required id="email"/>
  <br>
   Re-enter Email Address <input name="emailConfirm" type="email" id="confemail" value="@." onblur="confirmEmail()"/>
   <br>
<input type="submit" name="submit" value="Submit Data"/>
<br>
</form>

I have tried multiple times to try and work out the date/time validations from Stack Overflow but they can't seem to work. All the other validations are working. // JavaScript below.

function  imageClick(image)
{ 
   var myImage = document.getElementById("myImage"); 
   myImage.src = image.src;
   thediv = document.getElementById("fullImage");
   thediv.style.visibility = "visible";
};

function removeImage(thediv)
{ 
   var myImage = document.getElementById("myImage"); 
   myImage.src = "#";
   thediv.style.visibility = "hidden";
};

function confirmEmail() 
{
    var email = document.getElementById("email").value
    var confemail = document.getElementById("confemail").value
    if(email != confemail) 
    {
        alert('Email Not Matching!');
    }
}

function AgeValidation(ageOfCustomer)
{
   var date=saildates.split('/')[2];
   var d = new Date();
   var lipday=((d.getFullYear()-date)/4);
   var month = d.getMonth()+1;
   var day = d.getDate();
   var output = day + '/' + month + '/' + d.getFullYear();
   var age=((daydiff(parseDate(output), parseDate(saildates))-lipday)/365);
   if(age > 18.000 || age== 18.000)
   {
       return true;
   }
   else
   {
       alert('Your age should be 18 or greater than 18 yrs!!!');
   }
}

function parseDate(str)
{
    var mdy = str.split('/')
    return new Date(mdy[2], mdy[1]-1, mdy[0]);
}

function daydiff(first, second) 
{
    return (first-second)/(1000*60*60*24)
}

function SubmitForm()
{
     document.forms['contactus'].action='http://posttestserver.com/post.php';
     document.forms['contactus'].submit();

     document.forms['contactus'].action='contact.php';
     document.forms['contactus'].submit();
     return true;
}

Answer

nznoor picture nznoor · Jun 17, 2014

Take a look at this jsfiddle. The function which determine the age is follows.

function get_age(born, now) {
      var birthday = new Date(now.getFullYear(), born.getMonth(), born.getDate());
      if (now >= birthday) 
        return now.getFullYear() - born.getFullYear();
      else
        return now.getFullYear() - born.getFullYear() - 1;
    }