2011-01-31 110 views
1

試圖使用jQuery來做到這一點,我發現了一個名爲「真正的日期驗證」的插件。我似乎無法正確實施它。驗證無效日期/閏年

任何人都可以幫助我嗎?試圖驗證日期2-31- [whateveryear]不應該工作,以及閏年...

<?php 
$month = $_POST["mo"]; 
$day = $_POST["theDay"]; 
$yr = $_POST["year"]; 
if (!isset($_POST['submit'])) { // if page is not submitted; echo form 
?> 
<script type="text/javascript" src="jquery.js"> </script> 
<script type="text"> 
$(document).ready(function() { 
    var month = $('#mo').val(); 
    var day = $('#tDay').val(); 
    var year = $('#tYear').val(); 
    var date = new Date(month+"/"+day+"/"+year); 

    if(day == "29" && month == "02") { 
    if(year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) { 
    return true 
    } else { return false; } 
    } else if(month == (date.getMonth()+1) && day == date.getDate() && year == date.getFullYear()) { 
    return true; 
    } else { return false; } 
}); 
</script> 




<h2 style="margin:0; padding:0;">Date Selection</h2> 
<FORM method="POST" action="<?php echo $PHP_SELF;?>"> 
<select name="mo" id="mo"> 
    <option disabled="disabled">SELECT MONTH</option> 
    <option>January</option> 
    <option>February</option> 
    <option>March</option> 
    <option>April</option> 
    <option>May</option> 
    <option>June</option> 
    <option>July</option> 
    <option>August</option> 
    <option>September</option> 
    <option>October</option> 
    <option>November</option> 
    <option>December</option> 
</select> 
<select name="theDay" id="tDay"> 
    <option disabled="disabled">SELECT DAY</option> 
    <option>1</option> 
    <option>2</option> 
    <option>3</option> 
    <option>4</option> 
    <option>5</option> 
    <option>6</option> 
    <option>7</option> 
    <option>8</option> 
    <option>9</option> 
    <option>10</option> 
    <option>11</option> 
    <option>12</option> 
    <option>13</option> 
    <option>14</option> 
    <option>15</option> 
    <option>16</option> 
    <option>17</option> 
    <option>18</option> 
    <option>19</option> 
    <option>20</option> 
    <option>21</option> 
    <option>22</option> 
    <option>23</option> 
    <option>24</option> 
    <option>25</option> 
    <option>26</option> 
    <option>27</option> 
    <option>28</option> 
    <option>29</option> 
    <option>30</option> 
    <option>31</option> 
</select> 

<select name="year" id="tYear"> 
    <option disabled="disabled">SELECT YEAR</option> 
    <option>2011</option> 
    <option>2010</option> 
    <option>2009</option> 
    <option>2008</option> 
    <option>2007</option> 
    <option>2006</option> 
    <option>2005</option> 
    <option>2004</option> 
    <option>2003</option> 
    <option>2002</option> 
    <option>2001</option> 
    <option>2000</option> 
    <option>1999</option> 
    <option>1998</option> 
    <option>1997</option> 
    <option>1996</option> 
    <option>1995</option> 
    <option>1994</option> 
    <option>1993</option> 
    <option>1992</option> 
    <option>1991</option> 
    <option>1990</option> 
    <option>1989</option> 
    <option>1988</option> 
    <option>1987</option> 
    <option>1986</option> 
    <option>1985</option> 
    <option>1984</option> 
    <option>1983</option> 
    <option>1982</option> 
    <option>1981</option> 
    <option>1982</option> 
</select> 
<INPUT TYPE="submit" value="Send" name="submit" /> 
</FORM> 
<? 
} else { 
echo "You chose: " . " ". $month . " ".$day . ", ". $yr; 
} 
?> 
+0

確實$ PHP_SELF即使沒有register_globals工作?無論如何,用`$ _SERVER ['PHP_SELF']` – ThiefMaster 2011-01-31 16:24:51

回答

0

爲什麼不只是在PHP中做?

<?php 
if($_SERVER['REQUEST_METHOD'] == 'POST') 
{ 
    $day = $_POST["theDay"]; 
    $month = $_POST["mo"]; 
    $year = $_POST["year"]; 

    if(checkdate($month, $day, $year)) 
    { 
     echo 'Congratulations, you managed to enter a date that exists'; 
    } 
} 
?> 
+0

替換它這不檢查它是否1)無效日期(如2/29/2010)或閏年... – Jshee 2011-01-31 17:07:12

+0

它適用於上述兩種情況。如果輸入的日期無效,checkdate將返回false。包括2月29日閏年的有效日期將返回true,表示有效日期。 – 2011-03-01 17:02:21

5

不需要自己檢查。 JavaScript Date對象將爲您做到這一點。創建對象後,只需將性能比較,以你原來值:

我把這個在一個單獨的功能,因爲從一個現成的函數返回值是沒有意義的:

function validateDate() { 
    var month = +$('#mo').val() - 1; // Convert to numbers with "+" prefix 
    var day = +$('#tDay').val(); 
    var year = +$('#tYear').val(); 
    var date = new Date(year, month, day); // Use the proper constructor 

    return date.getFullYear() == year && date.getMonth() == month && date.getDate() == day; 
}