javascript - 当表单中的 onsubmit 事件只有第一个函数工作时,我想返回所有 3 个函数为 true
问题描述
用于检查电子邮件的 Ajax 代码是新的还是现有的
我希望这三个功能return(checkpass() && check() && validate(this))
都能正常工作。目前只有该功能checkpass()
有效。如果我写return(check() && checkpass() && validate(this))
,只有check()
函数被触发。
function check(){
var uname=document.forms["register_form"]["uname"].value;
var uemail=document.forms["register_form"]["uemail"].value;
var upassword=document.forms["register_form"]["upassword"].value;
var ucpassword=document.forms["register_form"]["ucpassword"].value;
if(uname=="" || uemail=="" || upassword=="" || ucpassword==""){
alert("all fields must be filled out");
return false;
}
}
function checkpass(){
var upass=document.forms["register_form"]["upassword"].value;
var ucpass=document.forms["register_form"]["ucpassword"].value;
if(upass!=ucpass){
alert("Confirm password should be same as password");
return false;
}
if(upass=="" && ucpass==""){
alert("cannot be kept blank");
return false;
}
}
function validate(useremail){
xhttp =new XMLHttpRequest();
xhttp.open("GET","emailvalidate.php?uemail="+useremail,true);
xhttp.send();
xhttp.onreadystatechange=function(){
if (xhttp.readyState == 4) {
if(xhttp.responseText==""){
document.getElementById("alert").innerHTML="cannot be empty";
return false;
}
else if(xhttp.responseText=="OK"){
document.getElementById("alert").innerHTML="<span class='badge badge-pill badge-primary'>welcome new user</span>";
}
else if(xhttp.responseText=="NO"){
document.getElementById("alert").innerHTML="<span class='badge badge-pill badge-primary'>Email Already Exist</span>";
return false;
}
else{
document.getElementById("alert").innerHTML="error happened";
return false;
}
}
};
}
<form method="post" action="register_action.php" id="register_form" name="register_form" onsubmit="return (checkpass() && check() && validate(this));">
<br>
<div class="form-group">
<label for="uname">Name:</label>
<input type="text" class="form-control" id="uname" placeholder="Enter Name " name="uname">
</div>
<div class="form-group">
<label for="uemail">Email id: </label>
<input type="email" class="form-control" id="uemail" placeholder="Enter Email ID" name="uemail"
onkeyup="javascript:validate(this.value)"><br>
<span id="alert"></span>
</div>
<div class="form-group">
<label for="upassword">Enter Password:</label>
<input type="password" class="form-control" id="upassword" placeholder="Set password" name="upassword">
</div>
<div class="form-group">
<label for="ucpassword">Confirm Password:</label>
<input type="password" class="form-control" id="ucpassword" placeholder="Enter password again" name="ucpassword" >
</div>
<button type="submit" class="btn btn-success">Submit</button>
</form>
解决方案
为什么不将它们包装在一个函数中:
var wrapper = function(){
return checkpass() && check() && validate(this);
}
接着
<form onsubmit="javascript:wrapper()">
此外,您可以对这个函数链进行 curry 以提高可读性,但是,上面的示例必须解决您的问题。
推荐阅读
- php - 带有多个参数的 URL 的 DOMDocument::createElementNS
- python - 使用 pyomo 解决 csv 文件中真实数据的运输问题
- python - Python 正则表达式 ip
- django - Wagtail 模型翻译不适用于 DEBUG = False
- c# - 从 global.asax.cs 文件中的控制器调用计时器上的函数
- android - Google Play 结算订阅:强制“保留”的新政策:应用 UI 中需要更改哪些内容?
- android - react-native-image-crop-picker 在单击图像后使应用程序崩溃
- linq - 如何使用 linq 查询 C# 获取值
- python - 计算熊猫列条目中给出的间隔的平均值
- bash - Bash printf:使用 Ctrl+U 清除整行