0

My JavaScript code is giving me a "declaration or statement expected error". It brings it up on the else statement. I'm using it to make a captcha for a small html feedback form.

Javascript:

function initCaptcha() {
    var captcha = generateCaptcha(),
        captchaAns = eval(captcha);
    
    $("#captcha")
    .attr("placeholder", captcha+" = ")
    .on("keyup", function() {
    if ($(this).val() !== "" && $(this).val() == captchaAns)
        $(".btn").removeClass("btn-disable");
        $(this).addClass("correct");
    else
        $(this).removeClass("correct");
        $(".btn").addClass("btn-disable");
    });
}

CSS:

.contact-form .btn {
    width: 180px;
    background-color: transparent;
    color: #e31d1a;
    font-size: 16px;
    border: 2px solid #e31d1a;
    padding: 0;
    margin-left: auto;
    cursor: pointer;
}

.contact-form .btn:hover {
    background-color: #e31d1a;
    transition: .5s linear;
    color: #f1f1f1;
}

.btn-disable {
    opacity: 0.65;
    cursor: not-allowed;
    pointer-events: none;
}

HTML:

<input required type="text" class="textb" placeholder="Name*">
<input required type="text" class="textb" placeholder="Phone Number*">
<input required type="text" class="textb" placeholder="Email*">
<textarea required placeholder="Tell Us How We Can Help*"></textarea>
<input required type="text" id="captcha" class="textb">
<input required type="submit" class="btn btn-disable" value='Send
Geshode
  • 3,600
  • 6
  • 18
  • 32
  • See https://stackoverflow.com/questions/359732/why-is-it-considered-a-bad-practice-to-omit-curly-braces?lq=1 – Barmar Jul 30 '20 at 14:55

1 Answers1

2

try this out, the error caused due to missing braces

if ($(this).val() !== "" && $(this).val() == captchaAns) {
    $(".btn").removeClass("btn-disable");
    $(this).addClass("correct");
  } else {
    $(this).removeClass("correct");
    $(".btn").addClass("btn-disable");
  }
Pandiyan Cool
  • 6,381
  • 8
  • 51
  • 87
  • A good answer should explain what the problem is and how the answer fixes it. Just posting code is not as informative. – Barmar Jul 30 '20 at 05:15
  • I can't see the difference between this and the code in the question. – Barmar Jul 30 '20 at 05:18