2013-06-05 79 views
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> 
+1

需要注意的是'parseInt函數(的document.getElementById( 「cmbmonth」)選項[。的document.getElementById( 「cmbmonth」)的selectedIndex] .value的);'可以' document.getElementById(「cmbmonth」)。value;'和你的鍵盤不會太快磨損。 :-) – RobG

回答

0

以年,月和日爲單位計算年齡比應該由月和年的長度差異來計算有點棘手。這裏有一個函數可以返回兩年中的日期,年,月,日,小時,分鐘和秒。

function dateDifference(start, end) { 

    // Copy date objects so don't modify originals 
    var s = new Date(+start); 
    var e = new Date(+end); 
    var timeDiff, years, months, days, hours, minutes, seconds; 

    // Get estimate of year difference 
    years = e.getFullYear() - s.getFullYear(); 

    // Add difference to start, if greater than end, remove one year 
    // Note start from restored start date as adding and subtracting years 
    // may not be symetric 
    s.setFullYear(s.getFullYear() + years); 
    if (s > e) { 
    --years; 
    s = new Date(+start); 
    s.setFullYear(s.getFullYear() + years); 
    } 
    // Get estimate of months 
    months = e.getMonth() - s.getMonth(); 
    months += months < 0? 12 : 0; 

    // Add difference to start, adjust if greater 
    s.setMonth(s.getMonth() + months); 
    if (s > e) { 
    --months; 
    s = new Date(+start); 
    s.setFullYear(s.getFullYear() + years); 
    s.setMonth(s.getMonth() + months); 
    } 


    // Get remaining time difference, round to next full second 
    timeDiff = (e - s + 999)/1e3 | 0; 
    days  = timeDiff/8.64e4 | 0; 
    hours = (timeDiff % 8.64e4)/3.6e3 | 0; 
    minutes = (timeDiff % 3.6e3)/6e1 | 0; 
    seconds = timeDiff % 6e1; 

    return [years, months, days, hours, minutes, seconds]; 
} 

您可以在年份部分之後縮寫以上所述,並且只需要返回即可。

注意,在你的代碼:

var cmbmonth=parseInt(document.getElementById("cmbmonth").options[document.getElementById("cmbmonth").selectedIndex].value); 

可以是:

var cmbmonth = document.getElementById("cmbmonth").value; 

沒有必要parseInt函數,Date構造會很樂意與字符串值工作。如果您已將日曆月份數字用於值(即Jan = 1),那麼在將其提供給Date構造函數之前減去1,但對於值使用javascript月份索引(即Jan = 0)更簡單。

那麼你可以這樣做:

var diff = dateDifference(new Date(cmbyear, cmbmonth, cmbdate), new Date()); 
if (diff[0] < 18) { 
    // sorry, under 18 
}