2014-03-12 81 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