Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6243519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:09:16+00:00 2026-05-24T12:09:16+00:00

I’m working on validating CAPTCHA input using AJAX. I realized I couldn’t return a

  • 0

I’m working on validating CAPTCHA input using AJAX. I realized I couldn’t return a value from the success handler on the AJAX call, so I created a global variable so the success handler and the validation function could talk to eachother. But it’s not working in the most mistifying way. First, the code:

jQuery('#contact').submit(function(event) {
        if (validate_form())
        {
            submit_form(event);
        }
        else
        {
            return false;
        }
    });

var valid = true;

function validate_form()
{
    valid = true;

    // test form stuff here
    // if something goes wrong valid will be set to false

    if (!valid)
    {
        alert("form is not valid, refreshing captcha");
        // refresh form
        // ....
    }
    else
    {
        alert("form is valid, checking captcha");
        check_recaptcha();
    }

    alert("finally, form is:" + valid);
    return valid;
}

function check_recaptcha()
{
    alert("checking captcha now");
    // take only recaptcha values from form
    var recaptcha_vals = jQuery(':input[name^="recaptcha"]').serialize();
    jQuery.post('recaptcha.php', recaptcha_vals, function(data) {
        alert("recaptcha post has succeeded!");
        if (data == '1\n' /*success!*/)
        {
            alert("recaptcha is correct!");
            jQuery('#recaptcha_error').empty();
        }
        else
        {
            alert("recaptcha is incorrect!");
            show_recaptcha();
            jQuery('#recaptcha_error').html("<p class=\"error\">CAPTCHA was incorrect, please try again.</p>");
            valid = false;
        }
    });
}

I tested this code with nothing in the CAPTCHA input field (an obviously incorrect value). According to the alerts, each part of the code went through as expected, except valid was still true after check_recaptcha() executes, even though I know the “incorrect” branch was followed. So question #1 is: Why isn’t the event handler able to set valid to false?

Here’s where it gets weird. In the above scenario, the form doesn’t submit, even though validate_form() returns true (according to the alerts). However, when I comment out all the alerts and try it again with the exact same inputs, then the form DOES submit. So question #2 is: What’s up with THAT? #1 is more important though.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T12:09:17+00:00Added an answer on May 24, 2026 at 12:09 pm

    The point of asynchronous callbacks is that the jQuery.post() will return immediately, without waiting for a reply from the recaptcha service. So when you make your “finally” alert, the recapthcha transation is still going on, and your callback will be called on when it is finished. Then, while your alert is waiting for you to click OK, the recaptcha reply arrives and your callback runs. If you comment out the alert, your submit handler will complete before the callback gets a chance to set valid to false.

    That’s why you need to supply a callback in the first place, rather than just reading a return value.

    Also: You are aware that checking a captcha on the client side is pointless, unless you repeat the check on the server side later, right? (Arguably it’s pointless even if you repeat it on the server. The point of client-side validation is to give the user instant feedback on trouble without waiting for a network roundtrip to the server. If you wait to submit until you’ve completed a roundtrip to check the captcha, you have accomplished nothing but doubling the response time in the successful case. The only exception I can think of is if your form contains really large amounts of data that would take appreciable time to transfer).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a text area in my form which accepts all possible characters from
I am using Paperclip to handle profile photo uploads in my app. They upload
I want use html5's new tag to play a wav file (currently only supported
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.