JavaScript出生日期验证



现在可以工作了。非常感谢!编辑我正在使用Dreamweaver和HTML工作,我想我在帖子中错误地选择了c#。

我正试图添加一个验证,当尝试提交时输出一个错误,如果D.O.B超过18岁。我不能让它与其他验证一起工作。

<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>

我已经多次尝试从堆栈溢出中找出日期/时间验证,但它们似乎无法工作。所有其他验证都在工作。//下面的JavaScript

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;
}

看一下这个jsfiddle。确定年龄的函数如下:

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;
    }

您给出的代码可能存在其他问题,但我注意到的第一个问题是函数AgeValidation不使用它的参数ageOfCustomer,而是使用未定义的saildates变量。

最新更新