现在正在运作。非常感谢! 编辑我正在使用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>
我已尝试多次尝试从Stack Overflow中计算出日期/时间验证,但它们似乎无法正常工作。所有其他验证都有效。 //下面的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;
}
答案 0 :(得分:1)
看一下 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;
}
答案 1 :(得分:0)
您提供的代码可能存在其他问题,但我注意到的第一个问题是函数AgeValidation
不使用它的参数ageOfCustomer
,而是使用undefined { {1}}变量。