在我的形式,我有這些元素[日] [月] [年]這使得用戶能夠輸入如何保持檢查的形式更新的Javascript
<form id="mail" action="form.php" method="post" onsubmit="validateForm()">
<label for="dateOfBirth">Date of Birth</label>
<input type="number" id="day" name="day" max="31" placeholder="Day" >
<input type="number" id="month" name="month" placeholder="Month" max="12" >
<input type="number" id="year" name="year" placeholder="Year" max="2016"/>
<input type="submit" value="submit">
</form>
當按鈕被按下提交,就應該檢查一下用戶超過18歲或沒有。如果他超過18歲,應提交表格。如果用戶未超過18歲,則應顯示錯誤。驗證年齡是工作的代碼,唯一的事情是,我不知道我應該怎麼把我的代碼
var day = getElementById("day").value;
var month = getElementById("day").value;
var year = getElementById("day").value;
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);
function validateForm() {
if ((currdate - setDate) > 0){
preventDefault(); // Prevent it being sent
var details = $('#mail').serialize(); // Serialize form data
$.post('form.php', details, function(data) { // Use $.post() to send it
$('#mail').html(data); // Where to display result
});
alert("above 18");
}else{
alert("below 18");
$("form").submit(function(e){
alert('submit intercepted');
e.preventDefault(e);
});
}
}
代碼來檢查年齡http://jsfiddle.net/Ttsa8/5/
附加一個事件監聽器來那些輸入....? – NewToJS
你的問題不清楚。更新你的問題詳細 –
你如何計算無效日期? – RobG