开发者

Validation of Invalid Date/ Leap Year

开发者 https://www.devze.com 2023-02-07 13:11 出处:网络
Trying to use jquery to do this, I found a plugin called \"real date validation\". I cant seem to implement it correctly.

Trying to use jquery to do this, I found a plugin called "real date validation". I cant seem to implement it correctly.

Can anyone help me? Trying to validate that the date 2-31-[whateveryear] shouldnt work, along with leap years...

<?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>
   &开发者_如何学Golt;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;
}
?>


No need to check that yourself. The JavaScript Date object will do that for you. After creating the object simply compare the properties to your original values:

I'm putting this in a separate function, because returning a value from a ready function doesn't make sense:

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;
}


Why not just do it in 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';
    }
}
?>


In case this helps others.

Using ISO (yyyy-mm-dd) date format, jQuery was accepting invalid dates where months were less than 31 days (2020-06-31) and did NOT handle leap year. Here is my solution which extends the date validator.

$.validator.methods.date = function (value, element) {
    if (value) {
        if (!jQuery.validator.methods.dateISO.call(this, value, element))
            return false;
        var jsDate = new Date(value);
        if (jsDate == "Invalid Date")
            return false;
        if (jsDate.toISOString().substring(0, 10) != value)
            return false;
    }
    return true;
}
0

精彩评论

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