2014-01-21 2 views
0

elated.com의 문의 양식을 사용 중입니다. 기본 수학 captcha를 구현하려고합니다. 그러나 양식을 제출할 때 captcha가 올 바르고 잘못 되었든간에 양식을 제출하는 대신 잘못된 captcha 오류 메시지가 나타납니다.javascript captcha 유효성 검사 부분

코드의이 부분은 내가 편집 한 하나입니다

function submitForm() { 
    var contactForm = $(this); 
    if (!$('#senderName').val() || !$('#senderEmail').val() || !$('#message').val()) { 

     $('#incompleteMessage').fadeIn().delay(messageDelay).fadeOut(); 
     contactForm.fadeOut().delay(messageDelay).fadeIn(); 

    } 
    // this is the part of the code I added the captcha verification. 
    if(('#captcha') != captcha_c) { 
     $("#captchaError").slideDown(500); 
     $("#captchaError").delay(messageDelay).slideUp(500); 
    } else { 

      $('#sendingMessage').fadeIn(); 
      contactForm.fadeOut(); 

      $.ajax({ 
      url: contactForm.attr('action') + "?ajax=true", 
      type: contactForm.attr('method'), 
      data: contactForm.serialize(), 
      success: submitFinished 
     }); 
    } 

    return false; 
} 

HTML 양식 : 나는 주석 부분을 제거하면 형태가 작동하고 이메일을 보내 진행되는

<form id="contactForm" action="processForm.php" method="post"> 
    <h2>Send us an email...</h2> 
    <ul> 
     <li> 
      <label for="senderName">Your Name</label> 
      <input id="senderName" maxlength="40" name="senderName" placeholder="Please type your name" required="required" type="text" /> 
     </li> 
     <li> 
      <label for="senderEmail">Your Email Address</label> 
      <input id="senderEmail" maxlength="50" name="senderEmail" placeholder="Please type your email address" required="required" type="email" /> 
     </li> 
     <li> 
      <label for="message" style="padding-top: .5em;">Your Message</label> 
      <textarea id="message" cols="80" maxlength="10000" name="message" placeholder="Please type your message" required="required" rows="10"></textarea> 
     </li> 
     <li> 
      <label id="captcha" for="captcha"></label> 
      <input id="captcha" name="captcha" placeholder="Enter the captcha" required="required" type="text" /> 
      <script type="text/javascript"> 
       generate_captcha('captcha'); 
      </script> 
     </li> 
    </ul> 
    <div id="formButtons"> 
     <input id="sendMessage" name="sendMessage" type="submit" value="Send Email" /> 
     <input id="cancel" name="cancel" type="button" value="Cancel" /> 
    </div> 
</form> 
<div id="sendingMessage" class="statusMessage"> 
    <p>Sending your message. Please wait...</p> 
</div> 
<div id="successMessage" class="statusMessage"> 
    <p>Thanks for sending your message! We&#39;ll get back to you shortly.</p> 
</div> 
<div id="failureMessage" class="statusMessage"> 
    <p>There was a problem sending your message. Please try again.</p> 
</div> 
<div id="incompleteMessage" class="statusMessage"> 
    <p>Please complete all the fields in the form before sending.</p> 
</div> 
<div id="captchaError" class="statusMessage"> 
    <p>Are you sure about your calculations?</p> 
</div> 

, 어디에서 javascript 부분에서 잘못 되었습니까? 이것에

if(('#captcha') != captcha_c) { 

:

if($('#captcha').val() != captcha_c) { 

은 또한 당신이 당신의 입력 때문에이를 변경 등의 라벨에 대해 동일한 ID를 가지고 여기

working live example

답변

1

변경이 있습니다 :

<label id="captcha" for="captcha"></label> 

<label id="something_else" for="captcha"></label> 
+0

이 제안 된 코드로 변경되었지만 작동하지 않음 – Nescafe

0

에 당신은 if(('#captcha') != captcha_c)('#captcha') 전에 $ 기호를 잊어 버렸습니다.

+0

이 제안 된 코드로 변경되었지만 작동하지 않음 – Nescafe

+0

console.log를 사용하여 $ ('# captcha') .all() 및 captcha_c를 인쇄 할 수 있습니까? –

+0

어떻게하면됩니까? 나는 PHP에서'var_dump()'를 사용할 수 있지만 javascript에서는 완전히 새로운 것임을 알고있다. – Nescafe