2014-03-12 38 views
0

我只是想知道如果任何人能够帮助我一个JavaScript验证程序,我试图做到这一点,以便在提交信息后验证ID号码被验证,到目前为止所有需要的是名称和主题,它将允许考官在没有输入考试ID号码的情况下继续进行,任何人都可以帮助我解决这个问题。验证信息已被输入

<script language="javascript"" type="text/javascript"> 

function validateForm() { 
      var result = true; 
      var msg=""; 

if (document.ExamEntry.name.value=="") { 
      msg+="You must enter your name \n"; 
      document.ExamEntry.name.focus(); 
      document.getElementById('name').style.color="red"; 
      result = false; 
} 

if (document.ExamEntry.subject.value=="") { 
      msg+="You must enter the subject \n"; 
      document.ExamEntry.subject.focus(); 
      document.getElementById('subject').style.color="red"; 
      result = false; 
} 

if (document.ExamEntry.subject.value=="") { 
      msg+="You must enter your Exam ID Number \n"; 
      document.ExamEntry.subject.focus(); 
      document.getElementById('Exam Number').style.color="red"; 
      result = false; 
} 


if(msg==""){ 
return result; 
} 
{ 
alert(msg) 
return result; 
      } 


} 
</script> 
<style type="text/css"> 
h1,h2,h3,h4,h5,h6 { 
      font-family: "Comic Sans MS"; 
} 
</style> 

<h1>Exam Entry Form</h1> 
    <table width="50%" border="0"> 
      <tr> 
          <td id="name">Name</td> 
          <td><input type="text" name="name" /></td> 
      </tr> 
      <tr> 
          <td id="subject">Subject</td> 
          <td><input type="text" name="subject" /></td> 
      </tr>    <tr> 
          <td id="Exam Number">Exam ID Number</td> 
          <td><input type="Number" name="ID Number"maxlength="4" >  </td> 
      </tr> 


      <tr> 
          <td><input type="submit" name="Submit" value="Submit"  onclick="return validateForm();" /></td> 
          <td><input type="reset" name="Reset" value="Reset" /></td> 
      </tr> 
</table> 
+0

这个是什么问题?你想验证最后一个字段吗? –

回答

0

以下是完整的HTML

<html> 
<head> 
    <script type="text/javascript"> 

     function validateForm() { 
      var result = true; 
      var msg = ""; 
      if (document.getElementById("txtname").value == "") { 
       msg += "You must enter your name \n"; 
       document.getElementById("name").focus(); 
       document.getElementById('name').style.color = "red"; 
       result = false; 
      } 

      if (document.getElementById("txtsubject").value == "") { 
       msg += "You must enter the subject \n"; 
       document.getElementById("subject").focus(); 
       document.getElementById('subject').style.color = "red"; 
       result = false; 
      } 

      if (document.getElementById("ID_Number").value == "") { 
       msg += "You must enter your Exam ID Number \n"; 
       document.getElementById("ID_Number").focus(); 
       document.getElementById('Exam Number').style.color = "red"; 
       result = false; 
      } 


      if (msg == "") { 
       return result; 
      } 
      else { 
       alert(msg) 
       return result; 
      } 


     } 
    </script> 
</head> 
<body> 
    <form id="ExamEntry"> 
    <h1> 
     Exam Entry Form</h1> 
    <table width="50%" border="0"> 
     <tr> 
      <td id="name"> 
       Name 
      </td> 
      <td> 
       <input type="text" id="txtname" /> 
      </td> 
     </tr> 
     <tr> 
      <td id="subject"> 
       Subject 
      </td> 
      <td> 
       <input type="text" id="txtsubject" /> 
      </td> 
     </tr> 
     <tr> 
      <td id="Exam Number"> 
       Exam ID Number 
      </td> 
      <td> 
       <input type="Number" id="ID_Number" maxlength="4"> 
      </td> 
     </tr> 
     <tr> 
      <td> 
       <input type="submit" name="Submit" value="Submit" onclick="return validateForm();" /> 
      </td> 
      <td> 
       <input type="reset" name="Reset" value="Reset" /> 
      </td> 
     </tr> 
    </table> 
    </form> 
</body> 
</html> 
+0

您能否将我的脚本复制并粘贴到一个线程中,并且大致显示它应该在的位置:) – JurshErMerGerd

+0

我在整个脚本标记中给出了 –

+0

它没有工作,我用代码替换了代码并保留了我,它只是继续下一页没有采取任何信息atall – JurshErMerGerd