javascript验证不起作用

时间:2013-12-06 06:29:29

标签: javascript html validation

validateType(), validateStatus(), validateMessage()不起作用。 有任何想法吗? validateEmail()validateName()有效。 我一直试图调试这一段时间没有任何运气。我已经筋疲力尽了。 感谢您的帮助! 该表格称为联系。

<html>
<head>
<title>Student Contact Form</title>
<link rel="stylesheet" type="text/css" href="homeStyle.css">
<script type="text/javascript">
function focus()  
{
    name1.focus();
}
function javaScriptValidation()
{  
if (validateName() == false)
      {
        return false;
      }
if (validateEmail() == false)
      {
        return false;
      }
if (validateSubject() == false)
      {
        return false;
      }
if (validateType() == false)
      {
        return false;
      }
if (validateStatus() == false)
      {
        return false;
      }
if (validateMessage() == false)
      {
        return false;
      }
return true;
}
function validateName()
{
var letters = /^[A-Za-z]+$/;
if (!letters.test(name1.value)) 
  {
    alert("Invalid Name (only letters)");
    name1.focus();
    return false;
  }
}
function validateEmail()
{
var letters = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (!letters.test(email.value))
    {
    alert("Invalid Email");
    email.focus();
    return false;
  }
}
function validateType()
{
var listSelection=document.forms["contact"]["type1"].value;
if(listSelection=="--Please Select--")
  {
  alert("You must select the reason to contact");
  return false;
  }
}
function validateStatus()
{
if ((contact.status[0].checked == false ) && (contact.status[1].checked == false )  && (contact.status[2].checked == false ) && (contact.status[4].checked == false)) 
{
  alert("Please select your status"); 
  return false;
}
}
function validateMessage()
{
var letters = /^[A-Za-z]+$/;
if (!letters.test(message.value)) 
  {
    alert("please write a message");
    message.focus();
    return false;
  }
}
</script>
</head>
<body onload="focus()">
    <div id = "banner">
            <a href = "Uparty.php"><img src = "images/logo03.jpg" alt = "Uparty Logo"/></a>
    </div>
<div id = "userAccount">
            <a href = "Uparty.php"> | HOME <img src = "images/home.png" alt ="event" style = "width:2%"/></a>
            <a href = "eventForm.php"> | CREATE EVENT! <img src = "images/event.png" alt ="event" style = "width:2%"/></a>
            <a href = "changePassword.php">| CHANGE PASSWORD <img src = "images/changePassword.jpg" alt = "change password icon" style = "width:2%"/></a>
            <a href = "contact.php"> | CONTACT US <img src = "images/contact.png" alt ="contact" style = "width:2%"/></a>
            <a href = "logout.php"> | LOG OUT <img src = "images/Log-Out-icon.png" alt = "logout icon" style = "width:2%"/></a>
</div>
<div id = "banner"> 
<img src = "images/Get in Touch.JPG" alt = "contact us"/>
<form name="contact" method="post" action="" onsubmit="return javaScriptValidation()">
<h2>WE'D LOVE TO HEAR FROM YOU</h2>
<p>Your Name [required] <br />
<input type="textbox" name="name" id="name1" class="boxes" size="40"/></p>
Your Email [required]<br />
<input type="textbox" name="email" id="email" class="boxes" size="40"/><br />
<p>Type: <select id="type1" class="type">
    <option value="--Please Select--" selected="selected">--Please Select--</option>
    <option value="support">Support</option>
    <option value="feedback">Feedback</option>
    <option value="general">General</option>
</select></p>
<p>College Statues-->
<input type="radio" name="status" value="freshman"/>Freshman
<input type="radio" name="status" value="Sophomore"/>Sophomore
<input type="radio" name="status" value="Junior"/>Junior
<input type="radio" name="status" value="Senior"/>Senior</p>
<p>Subject [required]<br />
<input type="textbox" name="subject" class="boxes" size="40"/><p>
<img src = "images/send a message banner.JPG" alt = "contact picture"/> <br />
Message [required]<br />
<textarea name="message" id="message" rows="15" cols="40"></textarea><br />
<input type="submit" class="submit" value="submit"/>
<input type="reset" class="reset" value="reset"/> 
</form>
<img src = "images/background.JPG" alt = "logout icon" style = "width:100%"/>
</div>
</body>
</html>

4 个答案:

答案 0 :(得分:0)

删除

if (validateSubject() == false)
      {
        return false;
      }

您尚未定义validateSubject()功能

答案 1 :(得分:0)

您没有名为validateSubject()

的功能

在这里被称为

if (validateSubject() == false)
      {
        return false;
      }

答案 2 :(得分:0)

function javaScriptValidation()
{  
  return validateName() && validateEmail() && validateSubject() && validateType() && validateStatus() && validateMessage();
}

添加ValidateSubject()实施并更改:

function validateName()
{
var letters = /^[A-Za-z]+$/;
if (!letters.test(name1.value)) 
  {
    alert("Invalid Name (only letters)");
    name1.focus();
    return false;
  }
   return true;  //add this line to each method
}

答案 3 :(得分:0)

请替换为以下js

function javaScriptValidation(form) {
    var namePattern = /^[A-Za-z]+$/;
    var emailPattern = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
// name
if (!namePattern.test(form.name.value)){
    alert("Invalid Name (only letters)");
    form.name.focus();
    return false;
}
//email
if (!emailPattern.test(form.email.value)){
    alert("Invalid Email");
    form.email.focus();
    return false;
}
//type
if(form.type.selectedIndex == 0){
    alert("You must select the reason to contact");
    form.type.focus();
    return false;
}
//status
if((form.status[0].checked == false) && (form.status[1].checked == false) && (form.status[2].checked == false) && (form.status[3].checked == false)){
    alert("Please select your status");
    return false;
}
//subject
if (!namePattern.test(form.subject.value)){
    alert("please write a subject");
    form.subject.focus();
    return false;
}
//message
if (!namePattern.test(form.message.value)){
    alert("please write a message");
    form.message.focus();
    return false;
}
return true;
}

为选择框添加名称     

<select id="type1" class="type" name="type">

在表单标签onsubmit验证上添加此项     

<form name="contact" method="post" action="" onsubmit="return javaScriptValidation(this)">