How to judge whether to submit data to the background only when all three input box conditions for registration are met

Keywords: Database PHP JSON

/**
*There are three input boxes on the registration page. You need all the conditions of the three input boxes to pass data to the server
*1. Wrap the main body with a function, and set three variables without parameters;
*2. Judge whether the data of each input box is correct, and return true if it is correct; return false if it fails; save the returned Boolean value with the three variables set at the beginning;
*3. When you click Register, first judge whether the password is entered correctly twice. If it is not, return. If it is correct, judge whether the Boolean values of the three variables are true. If it is true, submit the data;
*/
//Self executing function

(function(){
    //Set three variables to save the return value
    let a,b,c;
    $("#username").blur(function(){
        const reg = /^.{4,11}$/;
        //Get the data that needs to be submitted to the server interface for inspection
        const username = $(this).val();
        a = reg.test(username);
        //Find whether the database already has the user name currently entered
        $.post("http://localhost/Mall/check.php",{username},function(data){
            // Determine whether the user name length is4-11String
            if(a){
                // Determine whether the user name has been registered
                if(data.res_code === 1){
                    $(".notice").show().html("The user is already registered");
                }else{
                    $(".notice").hide().html("");
                }
            }else{
                $(".notice").show().html("Please enter at least 4 user names");

            };
        },"json");
        return a;
    })
    $("#password").blur(function(){
        const reg = /^.{6,11}$/,       
            password = $(this).val(),//Get the password that needs to be submitted to the database for checking
        b = reg.test(password);
            // Determine whether the password length is6-11String
            if(b){
                $(".notice").hide().html("");
            }else{
                $(".notice").hide().html("");
                $(".notice").show().html("Please enter a password of at least 6 digits");
            }
            console.log(b)
            return b;
    })
    $("#re_password").blur(function(){
        const password = $("#password").val(),//Get the password that needs to be submitted to the database for checking
            re_password = $(this).val();//Get the password entered again
        c = password===re_password; 
            //Determine whether the password entered for the second time is consistent with that of the first time
        if(c && re_password!=""){
            $(".notice").hide().html("");
        }else{
            $(".notice").show().html("Two password entries are inconsistent,Please re-enter");
        }
        console.log(c)
        return c;
    });
    //Click the register button
    $(".register_btn").click(function(){
        const username = $("#username").val(),
            re_password = $("#re_password").val(),
            password = $("#password").val();
    //Judge whether the passwords are the same again
            if(re_password != password){//Two password entries are inconsistent
                $(".notice").show().html("Two password entries are inconsistent,Please re-enter");
                return
            }else{//The two passwords are the same
                if(a&&b&&c){//a. b and c are all true
                    $.ajax({
                        url : "http://localhost/Mall/register.php",
                        type : "POST",
                        data : {username,},//"username="+username Query string
                        dataType : "json",
                        success : function(data){
                            console.log(data)   
                            if(data.res_code===1){
                                alert("login was successful");
                                location = "/html/login.html";
                            }
                        }
                    })
                }
            }
    });
})();

Posted by nabeelkhan on Sat, 04 Jan 2020 19:59:29 -0800