/**
? * 注冊頁面有三個輸入框,需要三個輸入框條件全部成立才能向服務器傳遞數(shù)據(jù)
? * 1冷冗、用一個函數(shù)將主體包裹起來吱窝,設置三個不傳參數(shù)的變量;
? * 2拦英、判斷每一個輸入框的數(shù)據(jù)是否正確蜒什,正確則返回true;失敗返回false疤估;用開始設置的三個變量保存返回的布爾值灾常;
? * 3霎冯、點擊注冊時,先再次判斷兩次密碼是否輸入正確钞瀑,不正確就return沈撞;正確就判斷三個變量的布爾值是否都為true,都為true則提交數(shù)據(jù)雕什;
? */
//自執(zhí)行函數(shù)
(function(){
//設置三個變量用于保存返回值
let a,b,c;
$("#username").blur(function(){
const reg = /^.{4,11}$/;
//獲取檢查需要向服務器接口提交的數(shù)據(jù)
const username = $(this).val();
a = reg.test(username);
//查找數(shù)據(jù)庫是否已有當前輸入的用戶名
$.post("http://localhost/Mall/check.php",{username},function(data){
// 判斷用戶名長度是否為4-11個字符串
if(a){
// 判斷用戶名是否已經(jīng)注冊
if(data.res_code === 1){
$(".notice").show().html("該用戶已注冊");
}else{
$(".notice").hide().html("");
}
}else{
$(".notice").show().html("請輸入至少4位的用戶名");
};
},"json");
return a;
})
$("#password").blur(function(){
const reg = /^.{6,11}$/,
password = $(this).val(),//獲取檢查需要向數(shù)據(jù)庫提交的密碼
b = reg.test(password);
// 判斷密碼長度是否為6-11個字符串
if(b){
$(".notice").hide().html("");
}else{
$(".notice").hide().html("");
$(".notice").show().html("請輸入至少6位的密碼");
}
console.log(b)
return b;
})
$("#re_password").blur(function(){
const password = $("#password").val(),//獲取檢查需要向數(shù)據(jù)庫提交的密碼
re_password = $(this).val();//獲取再次輸入的密碼
c = password===re_password;
//判斷第二次輸入的密碼是否和第一次的密碼一致
if(c && re_password!=""){
$(".notice").hide().html("");
}else{
$(".notice").show().html("兩次密碼輸入不一致,請重新輸入");
}
console.log(c)
return c;
});
//點擊注冊按鈕
$(".register_btn").click(function(){
const username = $("#username").val(),
re_password = $("#re_password").val(),
password = $("#password").val();
//再次判斷兩次密碼是否一致
if(re_password != password){//兩次密碼輸入不一致
$(".notice").show().html("兩次密碼輸入不一致,請重新輸入");
return
}else{//兩次密碼輸入一致
if(a&&b&&c){//a缠俺,b,c三個變量都為true
$.ajax({
url : "http://localhost/Mall/register.php",
type : "POST",
data : {username,},//"username="+username查詢字符串
dataType : "json",
success : function(data){
console.log(data)
if(data.res_code===1){
alert("注冊成功");
location = "/html/login.html";
}
}
})
}
}
});
})();