日期:2014-05-16  浏览次数:20649 次

怎样使用js来验证年龄是否大于18岁
<div class="renzhenglx_div">
  <div class="renzheng_left_div" style="line-height: 20px">
  生日:</div>
  <div class="renzheng_right_div">
  <select name="Date_Year" id="Date_Year" class="select_b_div" style="width: auto;">
  </select>&nbsp;年&nbsp;&nbsp;
  <select name="birthday_m" id="birthday_m" class="select_b_div" style="width: auto;">
  </select>&nbsp;月&nbsp;&nbsp;
  <select name="birthday_d"" id="birthday_d" class="select_b_div" style="width: auto;">
  </select>&nbsp;日
  </div>
  </div>

------解决方案--------------------
HTML code
<div class="renzhenglx_div">
  <div class="renzheng_left_div" style="line-height: 20px">
  生日:</div>
  <div class="renzheng_right_div">
  <select name="Date_Year" id="Date_Year" class="select_b_div" style="width: auto;">
  <script type="text/javascript">
      var i;
      for (var i = 1980; i <= 2000; i++) document.write('<option value="' + i + '">' + i + '</option>');
  </script>
  </select>&nbsp;年&nbsp;&nbsp;
  <select name="birthday_m" id="birthday_m" class="select_b_div" style="width: auto;">
  <script type="text/javascript">
      for (var i = 1; i <= 12; i++) document.write('<option value="' + i + '">' + i + '</option>');
  </script>
  </select>&nbsp;月&nbsp;&nbsp;
  <select name="birthday_d"" id="birthday_d" class="select_b_div" style="width: auto;">
  <script type="text/javascript">
      for (var i = 1; i <= 31; i++) document.write('<option value="' + i + '">' + i + '</option>');
      function checkage() {
          var y =parseInt( document.getElementById('Date_Year').value)
          , m = parseInt( document.getElementById('birthday_m').value)-1
          , d = parseInt(document.getElementById('birthday_d').value);
          var nowday = new Date(), birthday = new Date(y, m, d)
          , age = nowday.getFullYear() - birthday.getFullYear() - ((nowday.getMonth() < birthday.getMonth() || nowday.getMonth() == birthday.getMonth() && nowday.getDate() < birthday.getDate()) ? 1 : 0); ;
          alert(age)
      }
  </script>
  </select>&nbsp;日<input type="button" value="检查年龄" onclick="checkage()" />
  </div>
  </div>