/**
* 注册页面有三个输入框,需要三个输入框条件全部成立才能向服务器传递数据
* 1、用一个函数将主体包裹起来,设置三个不传参数的变量;
* 2、判断每一个输入框的数据是否正确,正确则返回true;失败返回false;用开始设置的三个变量保存返回的布尔值;
* 3、点击注册时,先再次判断两次密码是否输入正确,不正确就return;正确就判断三个变量的布尔值是否都为true,都为true则提交数据;
*/
//自执行函数
(function(){
//设置三个变量用于保存返回值
let a,b,c;
// 用户名框失去焦点时验证
(“#username”).blur(function(){
const reg = /^.{4,11}
(“#username”).blur(function(){ const reg = /^.{4,11}
/;
//获取检查需要向服务器接口提交的数据
const username =
(this).val();a=reg.test(username);//查找数据库是否已有当前输入的用户名
(
t
h
i
s
)
.
v
a
l
(
)
;
a
=
r
e
g
.
t
e
s
t
(
u
s
e
r
n
a
m
e
)
;
/
/
查
找
数
据
库
是
否
已
有
当
前
输
入
的
用
户
名
.post(“http://localhost/Mall/check.php“,{username},function(data){
// 判断用户名长度是否为4-11个字符串
if(a){
// 判断用户名是否已经注册
if(data.res_code === 1){
(“.notice”).show().html(“该用户已注册”);
}else{
(“.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(),//获取检查需要向数据库提交的密码
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(),//获取检查需要向数据库提交的密码
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三个变量是否都符合条件
$.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";
}
}
})
}
}
});
})();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84