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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:15:27+00:00 2026-06-17T05:15:27+00:00

I want to validate a form such that I can check the basics (such

  • 0

I want to validate a form such that I can check the basics (such as username entered, username a minimum length, etc.) but also such that I can ensure things like username and email have not already been (which requires a database search).

I have all the pieces, and after playing around have narrowed it down to find that the $.post statement, while it is meant to add to my ‘fail’ counter, is not doing so. How can I get some form of output from the $.post line so that I can truly validate (because right now, even if the username is not available, it will submit the form).

Here is my <script> (although for simplicity shortened to just show the username piece). Note that ‘usernameoff’ is my error block:

function validateForm()
{
   // set variables
   var username=document.forms["registrationnew"]["username"].value;
   var usernamespace=username.indexOf(" ");

   // set fail counter variable to determine if any requirements failed
   var counter=0;

   // check that username isn't blank, containing spaces, too short, or taken
   if (username==null || username=="")
   {
      counter++;
      $('#usernameoff').html("Must enter a username");
   }
   else if (username.indexOf(" ") >= 0)
   {
      counter++;
      $('#usernameoff').html("Cannot contain spaces");
   }
   else if (username.length < 3)
   {
      counter++;
      $('#usernameoff').html("Must be at least three characters");
   }
   else
   {
      $.post("check_username.php", {username: username}, function(result)
      {
         if(result == 0)
         {
            $('#usernameoff').html("Available");
         }
         else
         {
            counter++;
            $('#usernameoff').html("Not available");
         }
      });
   }  

   // if any test failed, return false
   if (counter > 0)
   {
      document.getElementById("errorflag").innerHTML=counter;
      return false;
   }
}

$.post conducts a query to determine whether or not the username entered has already been taken (and returns 0 or 1). My issue is that, regardless of which it returns, it does not increment ‘counter’, and therefore the form still submits.

  • 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-17T05:15:28+00:00Added an answer on June 17, 2026 at 5:15 am

    Your final test (if (counter > 0)) will run before your AJAX call has returned. This is the ‘asynchronous’ part of AJAX.

    The simplest solution, in your case, is to replace $.post with a synchronous call:

    $.ajax({
      type: "POST",
      url: "check_username.php", 
      data: {'username': username},
      async: false,
      success: function(result) { /* ... */ }
    });
    

    This will cause a delay while your script waits for the return, but since it’s on your own server, that delay should be fairly minimal.

    (Synchronous calls aren’t always possible. Read the docs for .ajax() for more.)

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

Sidebar

Related Questions

I want to validate a registration form that accepts an email address in the
I have a form that has target=_blank on submit. I want to validate the
I want to validate a form fields (text) input. It should only be a
Hi I want to validate a form input so it only takes the numbers
Im using asp.net webforms. c# I want to validate a form. In the codebehind
In Zend_Form i want to validate for duplicate records that might exist in database.
I want to post some values from a simple HTML form, validate them with
I'm trying to validate some of the field in my form. and I want
I have a form that validates the email address and I want to be
I have a form that I want to be validated before showing it initially.

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.