不知道为什么,同一个界面两个表单两个函数,但是就是只执行注册部分的表单验证后跳转,登录部分还是不验证直接跳转……
HTML表单相关代码
<div class="form-box">
<!--注册设计-->
<form class="register-box hidden" action="./zcjm.php" method="post">
<h2>注册新的账号</h2>
<div class="form-control">
<input type="text" id="Username" class="qkk" name="Username" placeholder="用户名" autocomplete="off">
<lable class="error-message"></lable>
</div>
<div class="form-control">
<input type="text" id="Phone" class="qkk" name="Phone" placeholder="手机号码" autocomplete="off">
<lable class="error-message"></lable>
</div>
<div class="form-control">
<input type="password" id="Password" class="qkk" name="Password" placeholder="密码" autocomplete="off">
<lable class="error-message"></lable>
</div>
<div class="form-control">
<input type="password" id="Confirm" class="qkk" placeholder="确认密码" autocomplete="off">
<lable class="error-message"></lable>
</div>
<input type="submit" class="btn" value="注册" onclick="return check()">
</form>
<!--登录设计-->
<form class="login-box" action="./dljm.php" method="post">
<h2>登录已有账号</h2>
<div class="form-control">
<input type="text" id="Usernamelog" class="qkk" placeholder="用户名" autocomplete="off" onblur="checkuser()">
<lable class="error-message"></lable>
</div>
<div class="form-control">
<input type="password" id="Passwordlog" class="qkk" placeholder="密码" autocomplete="off" onblur="checkpwd()">
<lable class="error-message"></lable>
</div>
<a href="#">忘记密码?</a>
<input type="submit" class="btn" value="登录">
</form>
</div>
JS相关代码
const Username = document.getElementById('Username');
const Phone = document.getElementById('Phone');
const Password = document.getElementById('Password');
const Confirm = document.getElementById('Confirm');
const Usernamelog = getElementById('Usernamelog');
const Passwordlog = getElementById('Passwordlog');
function check()
{
const UsernameValue = Username.value.trim();
const PhoneValue = Phone.value.trim();
const PasswordValue = Password.value.trim();
const ConfirmValue = Confirm.value.trim();
if(UsernameValue=="")
{
printError(Username,"用户名不可为空!");
return false;
}
else if(!validateUsername(UsernameValue))
{
printError(Username,"用户名不可少于4个字符!");
return false;
}
else
{
removeError(Username);
}
if(PhoneValue=="")
{
printError(Phone,"手机号码不可为空!");
return false;
}
else if(!validatePhone(PhoneValue))
{
printError(Phone,"手机号码无效");
return false;
}
else
{
removeError(Phone);
}
if(PasswordValue=="")
{
printError(Password,"密码不可为空!");
return false;
}
else if(!validatePassword(PasswordValue))
{
printError(Password,"密码必须由6-16位字母和数字组成!");
return false;
}
else
{
removeError(Password);
}
if(ConfirmValue=="")
{
printError(Confirm,"请再次输入以确认密码");
return false;
}
else if(PasswordValue !== ConfirmValue)
{
printError(Confirm, "两次密码不一致");
return false;
}
else
{
removeError(Confirm);
}
}
function check2()
{
const UsernamelogValue=Usernamelog.value.trim();
const PasswordlogValue=Passwordlog.value.trim();
if(UsernamelogValue=="")
{
printError(Usernamelog,"用户名不可为空!");
return false;
}
else
{
removeError(Usernamelog);
}
if(PasswordlogValue=="")
{
printError(Passwordlog,"密码不可为空!");
return false;
}
else
{
removeError(Passwordlog);
}
}
function printError(input,message)
{
const formContorl=input.parentElement;
formContorl.classList.add('error');
formContorl.querySelector('.error-message').textContent=message;
}
function removeError(input)
{
const formContorl=input.parentElement;
formContorl.classList.remove('error');
formContorl.querySelector('.error-message').textContent="";
}
function validateUsername(Username)
{
const Username_reg = /.{4,}/;
return Username_reg.test(Username);
}
function validatePhone(Phone)
{
const Phone_reg = /^((1[3,5,8][0-9])|(14[5,7])|(17[0,6,7,8])|(19[7]))\d{8}$/;
return Phone_reg.test(Phone);
}
function validatePassword(Password)
{
const Password_reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/;
return Password_reg.test(Password);
}
运行结果就是——只有注册的表单可以验证后跳转,登录的表单还是直接跳转并且在数据库中存储空值……
查了好多多表单单独验证的方法,但是都没有用……也不会ajax和vue,想请问只用js可不可以实现单独验证呢?非常感谢!
通过获取本地数据,然后遍历,登录输入不同账户密码进行匹配,有就跳转