为什么我的HTML / Javascript代码不起作用?

时间:2013-11-07 03:07:53

标签: javascript

这是我的代码,它不起作用,没有任何警报显示,似乎没有javascript工作。任何帮助将不胜感激。

<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Anyville University</title>

<script>
function nameCheck()
{
  if(input.firstName.value.length >= 2 && input.lastName.value.length >=2 &&
     input.firstName.value.match(/^[A-Za-z]+$/) && input.lastName.value.match(/     ^[A-Za-z]+$/)
   {
    return true;
   }
   else
   {
    return false;
   }
}

function emailCheck()
{
  if(input.email.value.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{1,5})+$/)
  {
   return true;
  }
  else
  {
   return false;
  }
}

function phoneCheck()
{
  if(input.phone.value.length == 10 && input.phone.value.match(/^[0-9]+$/))
   {
    return true;
   }
  else
  {
   return false;
  }
}

function ValidateForm()
{
  if(nameCheck == false)
  {
   alert("Invalid first or last name");
   input.firstName.focus();
  }

  if(emailCheck == false)
  {
   alert("Invalid email address");
   input.email.focus();
  }

  if(phoneCheck == false)
  {
   alert("Invalid phone number");
   input.phone.focus();
  }
}

</script>



</head>
<body>
<h1><b>Anyville University Feedback</b></h1>

<form name="input" action="/~mkamal/csc442/cgi-bin/thanks.cgi" method="get"
onsubmit="return ValidateForm();">
First Name: <input type="text" name="firstName" size="15" maxlength="20"><br>
Last Name: <input type="text" name="lastName" size="15" maxlength="20"><br>
Postal Address: <input type="text" name="postal" size="60" maxlength="60"><br>
E-Mail Address: <input type="text" name="email" size="25" maxlength="30"><br>
Telephone Number: <input type="text" name="phone" size="3" maxlength="3">-
                  <input type="text" name="phone" size="3" maxlength="3">-
                  <input type="text" name="phone" size="4" maxlength="4"><br>

<p>Which day did you visit campus?</p>
<select name="days">
<option value="Sunday" selected>Sunday</option>
<option value="Monday">Monday</option>
<option value="Tuesday">Tuesday</option>
<option value="Wednesday">Wednesday</option>
<option value="Thursday">Thursday</option>
<option value="Friday">Friday</option>
<option value="Saturday">Saturday</option>
</select>

<br>

<p>Select what you liked about campus: </p>
<input type="checkbox" name="like" value="students">Students<br>
<input type="checkbox" name="like" value="location">Location<br>
<input type="checkbox" name="like" value="campus">Campus<br>
<input type="checkbox" name="like" value="atmosphere">Atmosphere<br>
<input type="checkbox" name="like" value="dorm">Dorm Rooms<br>
<input type="checkbox" name="like" value="sports">Sports<br>

<p>How did you become interested in the University?</p>
<input type="radio" name="interest" value="parents">Parents<br>
<input type="radio" name="interest" value="friends">Friends<br>
<input type="radio" name="interest" value="tv">Television<br>
<input type="radio" name="interest" value="internet">Internet<br>
<input type="radio" name="interest" value="other">Other<br>

<textarea rows="5" cols="50">
Enter additional comments here.
</textarea>

<input type="submit" value="Submit">
<input type="reset" value="Reset">

</form>
</body>
</html>

我认为它可能与脚本有关,但我不确定。

1 个答案:

答案 0 :(得分:1)

在第11行中,您错过了)

if(input.firstName.value.length >= 2 && input.lastName.value.length >=2 &&
     input.firstName.value.match(/^[A-Za-z]+$/) && input.lastName.value.match(/     ^[A-Za-z]+$/) )  // <----- HERE

在第22行,您还会错过)

在函数ValidateForm

function ValidateForm()
{
  if(nameCheck() == false)
  {
   alert("Invalid first or last name");
   input.firstName.focus();
  }

  if(emailCheck() == false)
  {
   alert("Invalid email address");
   input.email.focus();
  }

  if(phoneCheck() == false)
  {
   alert("Invalid phone number");
   input.phone.focus();
  }
}