2012-08-09 3 views
1

내가 다음 JS 코드 한 제출 : 나는 갈 것이다 제출 누를 때, 나는 빈 양식을양식 유효성 검사가 실패하지만 여전히

<form method="post" action="forms/post.asp" onsubmit="valform();return false"> 
      <strong>Poker Username</strong> <span id="userfield" style="display:none;color:#F00">*</span><br /> 
      <input type="text" name="username" id="username" style="width:230px;" /> 
      <br /><br /> 
      <div class="vert"> 
       <strong>Email Address</strong> <span id="notsame" style="display:none;color:#F00">*</span><br /> 
       <input type="text" name="email" id="email" style="width:230px;" /> 
      </div> 
      <div class="vert2"> 
       <strong>Confirm Email Address</strong> <span id="notsame2" style="display:none;color:#F00">*</span><br /> 
       <input type="text" name="confemail" id="confemail" style="width:230px;" /> 
      </div> 
      <div style="clear:both;"></div> 
      <div class="espn"> 
       <div class="txt"><strong>Enter the ESPN special code</strong></div> 
       <input type="text" name="espncode" id="espncode" style="width:230px;" /> 
      </div> 
      <div id="nocode" style="display:none"> 
       You need to enter the code above 
      </div> 
      <div class="tc"> 
       <input type="checkbox" id="agree" name="agree" /> <strong>You agree to the terms and conditions of this sweepstake. Only one entry per person per sweepstake period. Any additional entries will be disqualified.</strong> 
      </div> 
      <br /> 
      <div class="submit"> 
       <input type="image" src="submit_BTN.png" /> 
      </div> 
     </form> 

떠나더라도 :

function checkFull(id) { 
    var input = $('#' + id).val(); 
    if (input == "") { 
     return false; 
    } 
    else { 
     return true; 
    } 
} 

function checkSame(id1,id2) { 
    var input1 = $('#' + id1).val();  
    var input2 = $('#' + id2).val(); 
    if (input1 == input2) { 
     return true; 
    } 
    else { 
     return false; 
    } 
} 
function showErrors() { 
    if (!checkFull('username')) { 
     $('#userfield').show(); 
    } 
    else { 
     $('#userfield').hide(); 
    } 

    if (!checkFull('email')) { 
     $('#notsame').show(); 
    } 
    else { 
     $('#notsame').hide(); 
    } 

    if (!checkFull('confemail')) { 
     $('#notsame2').show(); 
    } 
    else { 
     $('#notsame2').hide(); 
    } 

    if (!checkFull('espncode')) { 
     $('#nocode').show();  
    } 
    else { 
     $('#nocode').hide();  
    } 

    if (notSame('email','confemail')) { 
     $('#notsame2').show(); 
     $('#notsame').show(); 
    } 
    else { 
     $('#notsame2').hide(); 
     $('#notsame').hide(); 
    } 
} 
function valform() { 
    showErrors(); 
    if (checkFull('username') && checkFull('email') && checkFull('confemail') && checkSame('email','confemail') && checkFull('espncode')) { 
     form.submit(); 
     alert('success'); 
    } 
    else { 

     return false; 
    } 
} 

그리고 다음과 같은 형식을 을 통하여. 왜 그런가요?

+0

유효성 검사가 있습니까? – Prateek

+0

@pKs - 예''showErrors()''를 실행할 때 표시되는 '오류'가 초 단위로 표시되고 양식이 제출됩니다 – user838437

+0

처음 두 함수를 [this] (https : //gist.github.com/c6945009d34c9e1842c7) – elclanrs

답변

3

당신은 submit 핸들러 내부에 유효성 검사 함수에서 값을 반환해야합니다

onsubmit="valform();return false" 

가 있어야한다 :

onsubmit="return valform();" 

이 제출에서 양식을 방지 할 때마다 valform false를 반환합니다.

그런데 valform 함수 내에서 form.submit() 함수를 호출하는 것은 의미가 없습니다. 함수가 거짓을 반환하지 않으면 어쨌든 제출됩니다. 당신이 당신의 함수의 이름을 수정하는 경우가 작동

ReferenceError: Can't find variable: notSame

:

if (/*validity checks pass*/) { 
    alert('success'); 
} 
else { 
    return false; 
} 

는하지만, 코드 with this fiddle를 통해보고 나는 오류가 나타났습니다. 이러한 사소한 버그를 디버깅 할 수있는 개발자 도구를 사용해야합니다. Firebug에는 Firefox 및 Chrome/Safari 용 웹 검사기가 내장되어 있습니다. Opera에는 잠자리도 있습니다.

+0

고마워, 나는 지금 이걸 시도하고 여전히 양식을 제출 .. 어떤 아이디어? – user838437

+0

그러면 아마 오류가 유효합니다. 그게 네가하려는 의도대로하는거야? –

+0

양식 유효성을 검사하고''alert ("success");를 남겨 두었을 때 실행해야하는 부분에서''form.submit()''을 제거했습니다. 양식을 제출할 때 경고 메시지가 나타나지 않지만 양식이 여전히 제출되므로 수집 한 내용이 거짓으로 반환되지만 여전히 양식을 제출합니다 .. – user838437

1

가능한 원인 : 형태 onsubmit="valform();"

  • onsubmit="valform();return false" 변화.

시도해도 여전히 작동하지 않으면 알려주세요.

관련 문제