2013-03-28 3 views
2

다음 코드가 있으며 유효성 검사 규칙이 작동하지 않습니다. 도움을 요청하십시오! 활성 x 컨트롤이 나타나지 않는 것처럼 웹 브라우저는 javascript 코드를 인식하지 못합니다.자바 스크립트 유효성 검사 규칙이 작동하지 않습니다.

<HTML> 
<head> 
<title>Exam entry</title> 

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

var result = true; 

function validateForm() { 

    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.ExaminationNumber.value=="") { 
     msg+("You must enter the Examination Number \n"); 
     document.ExamEntry.ExaminationNumber.focus(); 
     document.getElementById('ExaminationNumber').style.color="red"; 
     result false; 
    } 
    if(msg==""){ 
    return result; 
    } 

    { 
    alert(msg) 
    return result; 
    } 
} 
</script> 
<form> 
     <input type="radio" name="qualification" value="GCSE">GCSE<br> 
     <input type="radio" name="qualification" value="AS">AS<br> 
     <input type="radio" name="qualification" value="A2">A2<br> 
</form> 
</head> 

<body> 
<h1>Exam Entry Form</h1> 
<form name="ExamEntry" method="post" action="success.html"> 
<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="ExaminationNumber">ExaminationNumber</td> 
     <td><input type="text" name="ExaminationNumber" /></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

당신은 '='라인 (23)에 대한 기호 29 result false;result = false;

팁해야 누락 : 당신은 당신의 브라우저의 개발자 콘솔 당신에게 JS 오류를 확인할 수 있습니다.

업데이트 : 좋아, 다음은 내가 그것을 테스트 할 때 잘 작동 :

<html> 
<head> 
    <title>Exam entry</title> 

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

     var result = true; 

     function validateForm() { 

      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.ExaminationNumber.value == "") { 
       msg + ("You must enter the Examination Number \n"); 
       document.ExamEntry.ExaminationNumber.focus(); 
       document.getElementById('ExaminationNumber').style.color = "red"; 
       result = false; 
      } 
      if (msg == "") { 
       return result; 
      } 

      { 
       alert(msg) 
       return result; 
      } 
     } 
    </script> 
</head> 

<body> 
    <h1>Exam Entry Form</h1> 
    <form name="ExamEntry" method="post" action="success.html"> 
     <table style="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="ExaminationNumber">ExaminationNumber</td> 
       <td> 
        <input type="text" name="ExaminationNumber" /></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

: 나는 getInputByName라는 도우미 함수를 추가? –

+0

그들은 아직 작동하지 않지만 더 이상 도움이되지 않습니까? 색상을 변경하지만 오류 메시지를 표시하지 않기 때문에 javascript가 실행 중입니까? –

+0

@SamanthaMclemon : 당신이'msg + = (...)'을 의미 할 때 당신은 몇 곳에서'msg + (...)'를 쓰고 있습니다. –

-1

질문의 코드가 실패하는 몇 가지 이유가 있습니다. 나에게 약간의 결함을 지적하자

  1. document.ExamEntry가 무효 인 머리 부분의 형태가 안된다. 맞을 것입니다 document.forms['ExamEntry']
  2. document.ExamEntry.subject과 같은 것이 불가능합니다. 하위 노드 연결은 작동하지 않습니다. 제공된 DOM 메소드를 사용하여 DOM을 탐색해야합니다. 나는 자바 스크립트를 수정하고 검증 작업을 만들기 위해 jsFiddle을 포함 시켰 등 http://net.tutsplus.com/tutorials/javascript-ajax/javascript-and-the-dom-series-lesson-1/

로 튜토리얼을 읽는 것이 좋습니다. 빠른 수정입니다. 난 내 원래의에서 수행 한하지만 여기에 일이 잘못 다른 일부 이유로

<script type="text/javascript"> 

function getInputByName(inputName) { 
    var inputs = document.getElementsByTagName('input'); 

    for (var i = 0, ii = inputs.length, input; i < ii, input = inputs[i]; i++) { 
     if (input.name === inputName) { 
      return input 
     } 
    } 
    return false; 
} 



function validateForm() { 

    var result = true; 

    var msg=""; 

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


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

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

    if (msg == ""){ 
     return result; 
    } else { 
     alert(msg) 
     return result; 
    } 
} 
</script> 
관련 문제