2014-10-28 121 views
-4

我使用的是twitter bootstrap中的日期選擇器,我想添加一些驗證,如果您的日期低於18歲,您的註冊將不會被認爲是已註冊的,我該如何去做?這裏是我的出生日期的示例html代碼:日期選擇器驗證18歲

<label>Date of Birth*</label> 
<input type="date" name="birth_date" tabindex="7" style="height: 30px;" required> 
+0

設置基於今天的日期的'min'屬性 - 18年(HTML5)。但是,無論如何,你必須在服務器端和/或js上檢查它。 – Cheery 2014-10-28 17:50:25

+0

最好的方法是進行JavaScript限制用戶體驗和PHP的安全限制。 – gr3g 2014-10-28 17:51:08

+0

對不起,但我不知道如何在JavaScript中做到這一點,我只是一個初學者在開發..所以對不起,有人會告訴我如何做到這一點? – user3819129 2014-10-28 17:53:23

回答

0

如果你看不起演示頁一點,你會看到一個「限制日期選擇器」一節。使用下拉菜單指定「年度下拉菜單可顯示最近20年」的演示,並創下查看源:

$("#restricting").datepicker({ 
    yearRange: "-20:+0", // this is the option you're looking for 
    showOn: "both", 
    buttonImage: "templates/images/calendar.gif", 
    buttonImageOnly: true 
}); 

你會想要做相同的(明顯變化-20至-100或東西)。

或者你可以使用

$('#dp1').datepicker({ 
    format: 'mm-dd-yyyy', 
    startDate: '-15d', 
    endDate: '+0d' // there's no convenient "right now" notation yet 
}); 
0

您需要使用javascript或您要使用的腳本語言驗證用戶輸入。

在PHP的一個例子是:

<?php 

if(isset($_POST['birth_date'])) 
{ 
    if($_POST['birth_date'] < 1996) 
    { 
     echo 'You can not register'; 
    } 

}