1

I'm trying to do a registration form. I'm checking 6 thing with three function if everything okey function returns true else false. First function checks Username length, username characters and username availability. Second function checks email validation and email availability. Third function checks pass match. Functions working good everything is okey until here. But I want to hide button at the first then if all the functions return true I want to show button. But It doesn't work.. Where am I wrong ? Thanks all :)

$(document).ready(function() {

    $("#rname").attr('maxlength','15');
    $("#rmail").attr('maxlength','50');
    $("#rpass").attr('maxlength','50');


//--------------------------------------------------//  MaxLenght Settings

    $("#rname").keyup(function() {

        var uname = checkUsername();

    });     // Username keyup

//--------------------------------------------------//  Username Checking

    $("#rmail").keyup(function() {

        var umail = checkEmail();

    });     // Email keyup

//--------------------------------------------------//  Email Checking

    $("#passone, #passtwo").keyup(function() {

        var upass = checkPass();

    });     // Email keyup

//--------------------------------------------------//  Password Checking

    $("#rname, #rmail, #passone, #passtwo").keyup(function() {

        var btn = button(uname,umail,upass);

    });     // button show hide

//--------------------------------------------------//  Button

});     //--------------------------------------------------//  Document Ready Ends

function checkUsername() {

    var chars = /^[a-zA-Z0-9\.\_]*$/;

    var username = document.getElementById("rname").value;

    if(chars.test(username) == true) {

        $("#notName").html("").show();

        if ((username.length > 3) && (username.length < 20)) {

            $("#notName").html("").show();

                $.post("check.php", { username: username },
                function(result){

                    if (result == true) {

                        $("#notName").html("").show();
                        return true;

                    }

                    else {

                        $("#notName").html("Username is already exists!").show();
                        return false;

                    }

                });

        }

        else {

            $("#notName").html("Username must be between 3 and 20 characters!").show();
            return false;

        }

    }

    else {

        $("#notName").html("Username can contain just A-Z, 0-9, dot and underscore!").show();
        return false;

    }

}

//--------------------------------------------------//  checkUsername Function

function checkEmail() {

    var email = document.getElementById("rmail").value;

    $.post("check.php", { vmail: email },
    function(result){

        if (result == true){

            $.post("check.php", { email: email },
            function(result){

                if (result == true) {

                    $("#notMail").html("").show();
                    return true;

                }

                else {

                    $("#notMail").html("Email is already exists!").show();
                    return false;

                }


            });

        }

        else {

            $("#notMail").html("Please enter a valid Email").show();
            return false;

        }


    });



}

//--------------------------------------------------//  checkEmail Function

function checkPass() {

    var passOne = document.getElementById("passone").value;
    var passTwo = document.getElementById("passtwo").value;

    if (passOne == passTwo) {

        $("#notPass").html("").show();
        return true;

    }

    else {

        $("#notPass").html("Passwords do not match!").show();
        return false;

    }

}

//--------------------------------------------------//  checkPass Function

function button(a,b,c) {

    if (a == true && b == true && c == true) {

        document.getElementById("button").style.display = "block";

    }

    else {

        document.getElementById("button").style.display = "none";

    }

}
yunusus
  • 67
  • 2
  • 7
  • Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Mike Cluck May 13 '16 at 22:10
  • But functions working not like that – yunusus May 13 '16 at 22:14
  • Yes it is. You're attempting to return the result of your `$.post` which is an asynchronous function. – Mike Cluck May 13 '16 at 22:18
  • Maybe you're right. I'm a new programmer. But my php file returns like that: echo true; echo false; And functions are working good just button function not working. – yunusus May 13 '16 at 22:21

1 Answers1

1

The main problem is that you didn't understand correctly how to use asynchronous function. You are using asynchronous functions as synchronous. This question can be of help to you.

jQuery Ajax POST example with PHP

Yet, just to try to keep you going and learn step-by-step, if you say that the only thing that isn't working is button function, it's possible that your code can work. Try using global variables instead of local, like this:

    $("#rname").keyup(function() {

        uname = checkUsername(); // var removed

    });     // Username keyup

//--------------------------------------------------//  Username Checking

    $("#rmail").keyup(function() {

        umail = checkEmail();

    });     // Email keyup // var removed

//--------------------------------------------------//  Email Checking

    $("#passone, #passtwo").keyup(function() {

        upass = checkPass(); // var removed

    });     // Email keyup that the variables can be read from all functions in the call to button()

About synchronous and asynchronous programming

Synchronous and asynchronous are relatively simple concepts.

Synchronous is the "normal" type of programming: one thing after the other. You call a function, it returns then the process goes on etc.

Asynchronous is when you call something (a server, web service, etc) and associate a function to that something that will be called when it finishes and you don't have control when that happens. So you don't have control of what comes first. Let's say you did 2 ajax calls and assigned to them two different functions. You can't be sure what function will run first. But nothing stops you from doing things like what you did before: create a wait loop that runs when these variables are set to true and make the functions update certain variables to exit the wait loop. You just have to consider that this return may never happen. There can be an error on the server and the ajax calls simply don't return or return an error. So you have to consider not to do an infinite loop in this case. Set an error function that set another variable to interrupt the cicle or something. Or make, say a time interval that if expired the loop is interrupted.

I hope this may have explained a bit the concepts to you.

Community
  • 1
  • 1
Nelson Teixeira
  • 6,297
  • 5
  • 36
  • 73
  • Actually didn't but I think it's about me. I just don't understand what is the difference between synchronous and asynchronous programming. What should I learn and where can I find informations with examples for my project. Because everyone's project is working different and at the first step unfortunality I can't understand with this examples I need an example with two programming style.. – yunusus May 14 '16 at 20:21
  • And the example I posted was useful ? – Nelson Teixeira May 14 '16 at 20:31
  • Not completely because it's a little complicated for me at the first but your answer was super! You told me my logic mistake. You told me "You are using asynchronous functions as synchronous." But I don't understand what I am actually doing. Can you easily edit my code with two simple example. If you can do it will so helpful for me. I can easily see my mistake and the other programming example on my code – yunusus May 14 '16 at 20:48