Javascript验证在XHTML中无效

时间:2013-11-08 15:44:50

标签: javascript

<?xml version = "1.0" encoding = "utf-8" ?>
<!DOCTYPE html PUBLIC "-//w3c//dtd xhtml 1.1//en"
   "http://www.w3.org/tr/xhtml11/dtd/xhtml11.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">

<head>
<title>Anyville University</title>

<script type="text/javascript">
<![CDATA[
function nameCheck()
{
  var fn=document.getElementById("firstName");
  if(fn.value.length >= 2 && fn.value.length >=2 &&
     fn.value.match(/^[A-Za-z]+$/) && fn.value.match(/^[A-Za-z]+$/))
   {
    return true;
   }
   else
   {
    return false;
   }
}

function emailCheck()
{
  var em=document.getElementById("email");
  if(em.value.match(/^\w+@\w\w\w\w\w+[.]\w\w+$/))
  {
   return true;
  }
  else
  {
   return false;
  }
}

function phoneCheck()
{
  var ph1=document.getElementById("phone1");
  var ph2=document.getElementById("phone2");
  var ph3=document.getElementById("phone3");
  if(ph1.value.length == 3 && ph2.value.length == 3 && ph3.value.length3 == 4 && 
ph1.value.match(/^\d+$/) && ph2.value.match(/^\d+$/) && ph3.value.match(/^\d+$/))
   {
    return true;
   }
  else
  {
   return false;
  }
}

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

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

  if(phoneCheck() == false)
  {
   alert("Invalid phone number");
   document.getElementById("phone1").focus();
  }
}
]]>
</script>



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

<form action="/~mkamal/csc442/cgi-bin/thanks.cgi"
onsubmit="return validateForm();" method="post">
<fieldset>
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="phone1" size="3" maxlength="3" />-
                  <input type="text" name="phone2" size="3" maxlength="3" />-
                  <input type="text" name="phone3" size="4" maxlength="4" /><br />

<p>Which day did you visit campus?</p>
<select name="days">
<option value="Sunday" selected="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" />
</fieldset>
</form>
</body>
</html>

它应该做的是在没有正确输入文本字段时显示警报,但无论我输入什么,都不会显示任何内容。我认为这个问题在剧本中,但我似乎无法弄明白。任何帮助将不胜感激。谢谢。

0 个答案:

没有答案