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

  • SEARCH
  • Home
  • 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 6240473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:34:38+00:00 2026-05-24T11:34:38+00:00

I have a javascript function that posts data to a validation script and grabs

  • 0

I have a javascript function that posts data to a validation script and grabs a value from there. The callback function on the post request returns a boolean value, and I’m trying to get the entire function to return that boolean value. Right now, the callback function returns the correct value, but the function itself doesn’t return anything. Here’s the code:

function validate(request_type, request_text) {
    $.post("http://www.example.com/ajax/validate.php",{
        type: request_type, 
        text: request_text
    }, function(data) {
        return (data == "valid");
    });
}

I realise that this is sort of a “synchronous” call, and that’s not what AJAX is about, but I already have numerous functions in validate.php (database calls, etc.) that I can’t implement in Javascript, and I saw threads like this one that talk about using some form of handler.

How would I write a simple handler that will make either the variable data or the result of the boolean comparison data == "valid" available when I use it in an if/else statement (which is where this function is supposed to be used)?

EDIT: For example, one of the if statements that will be using the boolean result:

if (!validate('password',pass_new)) {
        $('#pass_new').addClass('error');
        $('#pass_confirm_new').addClass('error');
        $(error_string.format('Please enter a valid password.')).insertAfter('#pass_confirm_new');
        $('#pass_text_short').hide();
        $('#pass_text_long').show();

EDIT: The function called with the onsubmit event in my HTML form:

function valid_pass_sett() {
    //code to remove errors left over from previous submissions - snipped
    pass_old = $('input[name=pass_old]').val();
    pass_new = $('input[name=pass_new]').val();
    pass_confirm_new = $('input[name=pass_confirm_new]').val();
    //some if statements that don't involve AJAX requests - snipped
    if (!validate('password',pass_new)) {
        $('#pass_new').addClass('error');
        $('#pass_confirm_new').addClass('error');
        $(error_string.format('Please enter a valid password.')).insertAfter('#pass_confirm_new');
        $('#pass_text_short').hide();
        $('#pass_text_long').show();
        return false;
    }

    return true;
}

I haven’t edited this code to include the updated code that’s been posted, but my question is how I return false from it to stop form submission?

  • 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-24T11:34:39+00:00Added an answer on May 24, 2026 at 11:34 am

    Unless you make a synchronous AJAX call (which you probably don’t want to do), you simply can’t.

    If this function is used in several places in your code, your best bet may be to allow it to receive a function.

    That way instead of relying on the result being returned from your function to be used in some code, you’re actually passing your code directly in, so it is ensured to be able to use the response.

    var my_form = $('#my_form');
    
    my_form.submit( valid_pass_sett );
    
    function valid_pass_sett() {
        //code to remove errors left over from previous submissions - snipped
        pass_old = $('input[name=pass_old]').val();
        pass_new = $('input[name=pass_new]').val();
        pass_confirm_new = $('input[name=pass_confirm_new]').val();
    
        validate('password', pass_new, pswd_validation_callback); // async validation
    
        return false;  // cancel form submission
    }
    
    function validate(request_type, request_text, callback ) {
        $.post("http://www.example.com/ajax/validate.php",{
            type: request_type, 
            text: request_text
        }, callback );
    }
    
    function pswd_validation_callback( data ) {
        if ( data === 'valid' ) {
             // if valid, call the native .submit() instead of the jQuery one
            my_form[ 0 ].submit();
        } else {
             // Otherwise do your thing for invalid passwords.
             // The form has already been canceled, so no concerns there.
            $('#pass_new').addClass('error');
            $('#pass_confirm_new').addClass('error');
            $(error_string.format('Please enter a valid password.')).insertAfter('#pass_confirm_new');
            $('#pass_text_short').hide();
            $('#pass_text_long').show();
        }
    }
    

    EDIT: Changed to use code posted in question.

    EDIT: Updating to work with additional code posted. Narrowing answer down to the named function for clarity.

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

Sidebar

Related Questions

I have a javascript function that manipulates the DOM when it is called (adds
I have a javascript function (class) that takes a function reference as one paremter.
I have a javascript function (function1) that checks some global variables (can the user
I have an external JavaScript that contains: function random_imglink(){ var myimages=new Array() myimages[1]="http://sevir.sitegoz.com/jchs/Banner1.png" myimages[2]="http://sevir.sitegoz.com/jchs/banner2.png"
Modern browsers have multi-tab interface, but JavaScript function window.showModalDialog() creates a modal dialog that
Ok, I have one JavaScript that creates rows in a table like this: function
I have this function in my Javascript Code that updates html fields with their
I have a calc function in java script that takes three integer parameters, following
I have a JavaScript function, pop_item . I have to call this from PHP,
I have a page that displays data from an object. I'm loading the object

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.