JavaScript中的年龄验证重定向

时间:2013-12-27 22:38:32

标签: javascript

我的老板要求我在我们的网站上添加年龄验证屏幕以便工作。

到目前为止,我有一个有效。但是,如果生日未满18岁,我需要将其重定向到Google,而不是说“你还不够老”(我只知道如何做到这一点)。

<head>

   <script language="javascript">
   function checkAge()
   {

   var min_age = 18;


   var year = parseInt(document.forms["age_form"]["year"].value);
   var month = parseInt(document.forms["age_form"]["month"].value) - 1;
   var day = parseInt(document.forms["age_form"]["day"].value);

   var theirDate = new Date((year + min_age), month, day);
   var today = new Date;

   if ( (today.getTime() - theirDate.getTime()) < 0) {
   alert("You are too young to enter this site!");
   return false;
   }
   else {

   return true;
   }
   }
   </script>

   **** PLEASE ENTER YOUR BIRTHDAY ****

   <form action="http://www.vaporwrx.com/default.asp" method="post" name="age_form">
   Day : <select name="day">
   <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>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>27</option>
   <option>29</option>
   <option>30</option>
   <option>31</option>
   </select>

   Month : <select name="month">
   <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>
   </select>

   Year : <select name="year">
   <option>2013</option>
   <option>2012</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>1980</option>
   <option>1979</option>
   <option>1978</option>

   </select>

   <input type="submit" name="senddate" value="Go" onClick="return checkAge()"> 
</head>

2 个答案:

答案 0 :(得分:0)

'''改变:

alert("You are too young to enter this site!");

为:

window.location.href = 'http://www.google.com/';

答案 1 :(得分:0)

if ( (today.getTime() - theirDate.getTime()) < 0) 
{
window.location = "http://www.google.com";
}