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 8402287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:01:01+00:00 2026-06-09T22:01:01+00:00

I have a small javascript function to validate my form on submit. With that

  • 0

I have a small javascript function to validate my form on submit.

With that i do some basic validation to check things are not empty. There is a also an ajax/json request that returns either a 1 or 0 if a person is over 18 or not.

all works fine except the json part will not return valid to the rest of the script, its just local to that part and for the life of me i cannot figure out how to get it to work.

function validate_form() {

    valid = true;

    //Set varialbles
    var firstname = $('#firstname').val();
    var lastname = $('#lastname').val();
    var email = $('#email').val();
    var comments = $('#comments').val();
    var day = $('#day').val();
    var month = $('#month').val();
    var year = $('#year').val();


    if ($('[name=photo]').val() == "") {
        $('#formtext1').fadeOut('fast', function() {
            $('#formtext2').fadeIn('fast', function() {
                // Animation complete.
            });
        });
        valid = false;
    }

    //check that terms have been accepted
    if (myForm.terms.checked == false) {
        $('#formtext1').fadeOut('fast', function() {
            $('#formtext2').fadeIn('fast', function() {
                // Animation complete.
            });
        });
        valid = false;
    }


    if (firstname != "" && lastname != "" && email != "" && comments != "" && day != "" && month != "" && year != "") {} else {
        $('#formtext1').fadeOut('fast', function() {
            $('#formtext2').fadeIn('fast', function() {
                // Animation complete.
            });
        });
        valid = false;
    }

    $.getJSON('mydomainhere.com/dob_validate/' + day + '/' + month + '/' + year, function(result) {    
        //Check dob
        var age = result.age

        if (age) {
            //Do nothing
        } else {
            valid = false;
        }
    });
    return valid;
}​
  • 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-06-09T22:01:03+00:00Added an answer on June 9, 2026 at 10:01 pm

    You need var valid=true; at the top (or outside to make it global) of your function. It looks like you are creating a local variable when you get the result back which goes out of scope after that code block.

    To make it async from my comment:

    $.ajax({
     url: 'mydomainhere.com/dob_validate/',
     dataType: 'json',
     async: false,
     data: day + '/' + month + '/' + year,
     success: 
          function(result) {
              var age = result.age 
              if (age) { } 
              else {
               valid = false;
              }
         }
    });
    

    http://www.w3schools.com/js/js_variables.asp

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

Sidebar

Related Questions

I have a small JavaScript function that (among other things) retrieves the ID of
I have a small javascript pricing calculation that is not working on a friend's
I have a pretty straight-forward javascript form validation script written: function validateForm(){ var x=document.forms[contactForm][firstname].value;
I have a small javascript function which opens an url in a new tab:
I have a JavaScript function that receives a parameter whose value is a fragment
I have a small question in JavaScript. Here is a declaration: function answerToLifeUniverseAndEverything() {
I made the following small Javascript script to enable some form elements on my
I have javascript function (that uses Raphael) that on click select area. And I
I have a small function that runs on load and window resize.. but the
I have a small script consisting of the code: <script type=text/javascript> function toggle_visibility(id) {

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.