【发布时间】:2015-01-28 07:26:46
【问题描述】:
编辑:我的问题措辞错误。如果我将所需的 DOB 字段留空,它会显示'Welcome',我希望它仅在用户 DOB 建议他们超过 17 岁时才说。在它说欢迎之后,它会告诉我填写所需的 DOB 字段。为什么没有输入 DOB 时还是说欢迎?
这是我认为相关的所有代码:
出生日期字段 出生日期:
<input type="text" name="Date" id="dateof" placeholder="YYYY-MM-DD" title="Enter your date of birth in the form YYYY-MM-DD" required /><br/><br/>
出生日期验证功能
var minAge = 17;
var today = new Date()
function calcAge(birthDate, todaysDate) {
todaysDate = new Date(today);
var givenDOB = document.getElementById('dateof').value; /*date of birth entered*/
var birthDate = new Date(givenDOB);
var years = (todaysDate.getFullYear() - birthDate.getFullYear());
if (todaysDate.getMonth() < birthDate.getMonth() ||
todaysDate.getMonth() == birthDate.getMonth() && todaysDate.getDate() < birthDate.getDate()) {
years--;
}
return years;
}
function setAge() {
var age = calcAge();
if (age < minAge) {
alert("Sorry, the minimum age is 17!");
} else
alert("Welcome!");
}
【问题讨论】:
-
您的浏览器可能或尝试输入
required=required -
你能分享你所有的表单代码吗?
标签: javascript forms