[jQuery] Form Client and server side validaion probleme

[jQuery] Form Client and server side validaion probleme


Hello,
I'm trying to fix a validation form probleme but I don't have any
idea.
This is the probleme :
I have a forum with inputs, for the validation, I have some client
side controls and server ones with an ajax call.
this is the code :
--------------------------------------------------------------------------------------------------------------------------------------
    $("#Register").submit(function() {
            $(".ErrorDiv").hide();
            $(".SuccessDiv").hide();
            var msg_error = 0;
     var valid_email = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})
+$/;
     var valid_text = /^\w+$/;
     var username = $("#Username").val();
     var email = $("#Email").val();
     var pwd = $("#Password").val();
     var pwd_conf = $("#PasswordConf").val();
     if ( !username.match(valid_text) || username == "" ) {
                msg_error = 1;
         $("#Username_Error").show();
            } else {
                $.ajax({
         type: "POST",
         url: "inc/user_reg.hand.php",
         data: "action=check_username&username="+username,
         dataType: "html",
         success: function(msg){
                        if (msg == "KO") {
                            msg_error = 1;
                     $("#UsernameUsed_Error").show();
                        }
         }
     });
            }
     if ( !email.match(valid_email) || email == "" ) {
                msg_error = 1;
         $("#Email_Error").show();
            } else {
                $.ajax({
         type: "POST",
         url: "inc/user_reg.hand.php",
         data: "action=check_email&email="+email,
         dataType: "html",
         success: function(msgmail){
                        if (msgmail == "KO") {
                            msg_error = 1;
                     $("#EmailUsed_Error").show();
                        }
         }
     });
            }
     if ( !pwd.match(valid_text) || pwd == "" ) {
         msg_error = 1;
         $("#Password_Error").show();
            }
     if ( pwd != pwd_conf ) {
         msg_error = 1;
         $("#PasswordConf_Error").show();
            }
     if( msg_error == 0 ) {
     $.ajax({
         type: "POST",
         url: "inc/user_reg.hand.php",
         data: "action=register&username="+username
+"&email="+email+"&password="+pwd,
         dataType: "html",
         beforeSend: function(msgregister){
         },
         success: function(msgregister){
                        $("#Register_Success").show();
                        bindStyle();
         }
     });
            }
     return false; // cancel conventional submit
    });
--------------------------------------------------------------------------------------------------------------------------------------
The thing is : when I try the ajax call the first time with the
'Username' and after a 'KO' response, the variable msg_error is still
on '0' and not '1' even if the msg received after the call is 'KO'
and the "#UsernameUsed_Error" is showed.
Can anyone help ?? :)