开发者

Validation of Specific Date using Php or Javascript

I need a quick way to validate in php or javascript whether a date is an invalid date such as (2/31 or 04/31 - since they dont exist), ALONG with if it is a LEAP year or not.

My code:

<?php 
$month = $_POST["mo"];
$day = $_POST["theDay"];
$yr = $_POST["year"];
if (!isset($_POST['submit'])) { // if page is not submitted; echo form
?>

<h2 style="margin:0; padding:0;">Date Selection</h2>
    <span style="color:gray; font-size:0.8em;">By Joseph Dickinson</span>
<FORM method="P开发者_JAVA百科OST" action="<?php echo $PHP_SELF;?>">   
    <select name="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">
      <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">
      <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;
}
?>

If someone could please help me out here.


First, add numbers as values for months:

<select name="mo">
  <option disabled="disabled">SELECT MONTH</option>
  <option value="1">January</option>
  <option value="2">February</option>
  <option value="3">March</option>
  <option value="4">April</option>
  <option value="5">May</option>
  <option value="6">June</option>
  <option value="7">July</option>
  <option value="8">August</option>
  <option value="9">September</option>
  <option value="10">October</option>
  <option value="11">November</option>
  <option value="12">December</option>
</select>

Then use checkdate():

<?php 

$month = $_POST["mo"];
$day = $_POST["theDay"];
$yr = $_POST["year"];
if (checkdate($month, $day, $yr) !== true)  {
    // ?
}

?>

And if you want to do it in the browser as well, first add an onsubmit handler to the year form:

<FORM method="POST" action="<?php echo $PHP_SELF;?>" onsubmit="return checkLeap(this);">

Then your function:

function checkLeap(form) {
    var month = form.mo.options[form.mo.selectedIndex].value;
    var day = form.theDay.options[form.theDay.selectedIndex].value;
    var year = form.year.options[form.year.selectedIndex].value;
    if (month == 2 && day == 29 && (new Date(year,month,year).getDate() != 29)) {
        alert('That is not a leap year. Please fix.');
        return false;
    }
    return true;
}


When you create a timestamp with mktime() with a date too high for the month, it just skips up to the next month, with a date of the difference. For example, Feb 31 produces Mar 3. So to make sure it's valid, just create the time, and then re-check that the month hasn't changed.

$month = 2;
$date = 31;
$year = 2011;
$time = mktime(1, 0, 0, $month, $date, $year);
if (date('n', $time) != $month) {
    // invalid error!
}

The same mechanism also automatically accounts for leap years. But if you want to know if it's a leap year or not, just check with date('L', $time);.

http://php.net/date

http://php.net/mktime

As a side note, if you do validate it with Javascript, you must also check with PHP. Never rely on client-side validation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜