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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:52:37+00:00 2026-06-18T11:52:37+00:00

In this simplified example of a larger web app, consider a simplistic registration form

  • 0

In this simplified example of a larger web app, consider a simplistic registration form with fields: username, firstname, lastname and a Register button type="button".

<form action="" method="post" id="cns_form">
    <table id="companyTable"><tr>
        <td width="200">
            First name*:<br />
            <input type="text" id="first_name" name="first_name">
        </td>
        <td width="200">
            Last name*:<br />
            <input type="text" id="last_name" name="last_name">
        </td>
    </tr></table>
    <input type="button" value="Register" id="register" >
</form>
<div id="alert" title="Alert"></div>

When the username field is completed, jQuery fires an ajax search of a database to see if that username already exists. This same search is also triggered when one clicks Register (for reasons removed from this simplified example).

PROBLEM: Everything works great when leaving the username field. However, after clicking Register, I don’t know how to retrieve the result of the AJAX search and stop the form from submitting if the username already exists. I’ve tried all kinds of different things, but have returned the code to this state so it is easiest for the reader to assist.

For example, I tried integrating the suggested solution from this question, but I was unsuccessful applying it to my situation… I tried setting async:false inside the ajax function… I also tried calling the checkUsername(uname) from inside the checkForm function, but that didn’t work either. A little help?

jQuery document.ready:

$(function(){
    $('#username').blur(function() {
        var uname = $.trim($(this).val());
        checkUsername(uname);
    }); //END BLUR username

    $('#register').click(function() {
        var uname = $.trim($( '#username').val());
        checkUsername(uname);
        checkForm();
    });
}); //END document.ready

AJAX Call:

function checkUsername(uname) {
        if (uname != '') {
            $.ajax({
                type: "POST",
                url: 'ajax/ax_all_ajax_fns.php',
                data: 'request=does_this_username_already_exist&username=' + uname,
                async: false,
                success:function(data){
//alert('Returned AJAX data: '+data);
                    if (data != 0) {
                        var existing_user = data.split('|');
                        var fn = existing_user[0];
                        var ln = existing_user[1];
                        focus_control = 'username';
                        $( '#alert' ).html( 'That username is already in use by ' + fn +' '+ ln +'. Please choose another.' );
                        $( '#alert' ).dialog( 'open' );
                    } //EndIf data<>0
                } //End success
            }); //End $.ajax
        } //End If this.val <> ""
}

checkForm Function:

function checkForm() {
    var un = $.trim($( '#username').val());
    var fn = $( '#first_name').val();
    var ln = $( '#last_name').val()

    if (un=='' || fn=='' || ln=='') {
        $( '#alert' ).dialog({
            height: 200,
            width: 300,
        });
        $( '#alert' ).html( 'Fields marked with an asterisk are required.' );
        $( '#alert' ).dialog( 'open' );
    } else {
        $("#cns_form").submit();
    }
}
  • 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-18T11:52:38+00:00Added an answer on June 18, 2026 at 11:52 am

    One both rejoices and weeps when answering his own question, but here goes. The solution was to send the checkUsername() function as an input param to the checkForm() function, and to make the checkUserName() function return a value that we could check inside checkForm().

    Therefore, we must modify the $('#register').click function thusly:

    $('#register').click(function() {
        var uname = $.trim($( '#username').val());
        checkForm(checkUsername(uname)); //<===========================
    });
    

    THEN the checkUsername() function, thus:

    function checkUsername(uname) {
            var returnVal = 0; //<=================================
            if (uname != '') {
                $.ajax({
                    type: "POST",
                    url: 'ajax/ax_all_ajax_fns.php',
                    data: 'request=does_this_username_already_exist&username=' + uname,
                    async: false,
                    success:function(data){
    //alert('Returned AJAX data: '+data);
                        if (data != 0) {
                            var existing_user = data.split('|');
                            var fn = existing_user[0];
                            var ln = existing_user[1];
                            focus_control = 'username';
                            $( '#alert' ).html( 'That username is already in use by ' + fn +' '+ ln +'. Please choose another.' );
                            $( '#alert' ).dialog( 'open' );
                            returnVal = 0; //<============================
                        } //EndIf data<>0
                    } //End success
                }); //End $.ajax
            } //End If this.val <> ""
        return returnVal; //<==============================
    }
    

    AND the checkform() function thus:

    function checkForm(exists) { //<============================
    alert('sub checkForm(). value of exists: ' + exists);
        if (exists==9) { //<================================
            $( '#alert' ).html( 'That username is already in use' + existing +'. Please choose another.' );
            $( '#alert' ).dialog( 'open' );
        }else{ //<==========================================
            var un = $.trim($( '#username').val());
            var fn = $( '#first_name').val();
            var ln = $( '#last_name').val()
    
            if (un=='' || fn=='' || ln=='') {
                $( '#alert' ).dialog({
                    height: 200,
                    width: 300,
                });
                $( '#alert' ).html( 'Fields marked with an asterisk are required.' );
                $( '#alert' ).dialog( 'open' );
            } else {
                $("#cns_form").submit();
            }
        } //<===================================================
    }
    

    Thanks and kudos to Felix Kling for this helpful post.

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

Sidebar

Related Questions

This example is simplified a bit, but in my ASP.NET web page in my
I have a larger SQL that produces a similar output as this simplified example:
Consider this simplified example: #include <list> typedef std::list<int> IntList; class KindaIntList { public: IntList::const_iterator
Just created an acc on SO to ask this :) Assuming this simplified example:
This is a simplified example to illustrate the question: class A {}; class B
I've simplified this example as much as I can. I have a remote function:
I cannot find the solution for this problem, here is the simplified example: On
I need some help putting together this query in Django. I've simplified the example
I've simplified my JavaScript code to the example below; this code is giving me
Let's say I have a bunch of text like this (simplified example, but you

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.