0
嘿javascript主人,Javascript - 年齡驗證
試圖爲客戶的網站創建年齡驗證頁。下面的代碼不起作用,因爲它不會影響您選擇哪一年,它仍然會允許您進入該網站。不知道我應該看什麼來糾正。
任何幫助表示讚賞。
<script type="text/javascript"><!--
function checkAge(f){
var dob=new Date();
var date=dob.getDate();
var month=dob.getMonth() + 1;
var year=dob.getFullYear();
var cmbmonth=parseInt(document.getElementById("cmbmonth").options[document.getElementById("cmbmonth").selectedIndex].value);
var cmbday=parseInt(document.getElementById("cmbday").options[document.getElementById("cmbday").selectedIndex].value);
var cmbyear=parseInt(document.getElementById("cmbyear").options[document.getElementById("cmbyear").selectedIndex].value);
age=year-cmbyear;
if(cmbmonth>month){age--;}
else{if(cmbmonth==month && cmbday>=date){age--;}}
if(cmbmonth==0){alert("You must enter the month you were born in.");return false;}
else if(cmbday==0){alert("You must enter the day you were born on.");return false;}
else if(cmbyear==2005){alert("You must enter the year you were born in.");return false;}
else if(age<13){alert("You are unable to view this site!");location.replace("http://www.dharmatalks.org");return false;}
else{return true;}
}
// --></script>
需要注意的是'parseInt函數(的document.getElementById( 「cmbmonth」)選項[。的document.getElementById( 「cmbmonth」)的selectedIndex] .value的);'可以' document.getElementById(「cmbmonth」)。value;'和你的鍵盤不會太快磨損。 :-) – RobG