Javascript Mousedown仍在提交表单,但不应提交表单
您好,我有一个登录验证表单,它混合使用jquery和ajax进行验证。。。如果值正常,则表单应提交,如果值不正常,则表单不应提交。。。但是,在我的情况下,即使值不正确,表单仍在提交(我使用的是mousedown函数),请参见下面的代码Javascript Mousedown仍在提交表单,但不应提交表单,javascript,php,jquery,ajax,forms,Javascript,Php,Jquery,Ajax,Forms,您好,我有一个登录验证表单,它混合使用jquery和ajax进行验证。。。如果值正常,则表单应提交,如果值不正常,则表单不应提交。。。但是,在我的情况下,即使值不正确,表单仍在提交(我使用的是mousedown函数),请参见下面的代码 <form method="post" name="loginform" action="models/login.php"> <input type="email" class="homepage" name="user_email2"
<form method="post" name="loginform" action="models/login.php">
<input type="email" class="homepage" name="user_email2" id="user_email2" placeholder="Email" maxlength="50" />
<div class="errormsg" id="errormsg6"></div>
<input type="password" class="homepage" name="user_password2" id="user_password2" placeholder="Password" maxlength="20" />
<div class="errormsg" id="errormsg7"></div>
<input type="submit" name="login" id="login" value="Submit">
<div class="errormsglast" id="errormsg8"></div>
</form>
jquery和ajax
$(document).ready(function()
{
/* ----------------- Login Validations Global Variables ----------------- */
var user_email2 = "";
var user_emailajax2 = "";
var user_password2 = "";
var user_passwordajax2 = "";
var emailformat = new RegExp(/^[+a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i);
/* ----------------- Define Validate Email */
var validate_email_login = function()
{
var item5 = $("#user_email2").val().toLowerCase();
if (item5.length < 6 || item5.length > 50)
{
$("#errormsg6").html("Email : 6 - 50 Characters");
user_email2 = "";
}
else
{
$("#errormsg6").html("");
user_email2 = item5;
if (!emailformat.test(item5))
{
$("#errormsg6").html("Wrong Email Format");
user_email2 = "";
}
else
{
$("#errormsg6").html("");
user_email2 = item5;
$.ajax(
{
type: 'POST',
url: 'classes/validatelogin.php?f=1',
data: "user_email2=" + item5,
success: function(msg)
{
if (msg == "ok")
{
user_emailajax2 = "";
$("#errormsg6").html("Email Does Not Exist");
}
else if (msg == "exists")
{
user_emailajax2 = item5;
$("#errormsg6").html("");
}
}
});
}
}
}
/* ----------------- Define Validate Password */
var validate_password_login = function()
{
var item5 = $("#user_email2").val().toLowerCase();
var item6 = $("#user_password2").val();
if (item6.length < 8 || item6.length > 20)
{
$("#errormsg7").html("Password : 8-20 Characters");
user_password2 = "";
}
else
{
$("#errormsg7").html("");
user_password2 = item6;
if (user_email2 != "" && user_emailajax2 != "")
{
$.ajax(
{
method: "POST",
url: "classes/validatelogin.php?f=2",
data: "user_email2=" + item5 + "&user_password2=" + item6,
success: function(msg)
{
if (msg == "WrongPw")
{
user_passwordajax2 = "";
$("#errormsg7").html("Wrong Password - See Forgot Password");
}
else if (msg == "CorrectPw")
{
user_passwordajax2 = item6;
$("#errormsg7").html("");
/* window.location.href="manage-properties"; */
}
}
});
}
}
}
/* ----------------- Run Functions */
$("#user_email2").on('focusout', validate_email_login);
$("#user_password2").on('focusout', validate_password_login);
/* ----------------- Stop on Submit */
$( "#login" ).mousedown(function()
{
validate_email_login();
validate_password_login();
if (user_email2 == "" || user_emailajax2 == "" || user_password2 == "" || user_passwordajax2 == "")
{
$("#errormsg8").html("Please Fill All Fields (Correctly)");
console.log("submit false");
return false;
}
else
{
$("#errormsg8").html("");
console.log("submit true");
return true;
}
});
});
$(文档).ready(函数()
{
/*--------------登录验证全局变量----------------*/
var user_email2=“”;
var user_emailajax2=“”;
var user_password2=“”;
var user_passwordajax2=“”;
var emailformat=newregexp(/^[+a-zA-Z0-9.-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i);
/*----------------定义验证电子邮件*/
var validate\u email\u login=函数()
{
var item5=$(“#user_email2”).val().toLowerCase();
如果(项目5.length<6 | |项目5.length>50)
{
$(“#errormsg6”).html(“电子邮件:6-50个字符”);
用户_email2=“”;
}
其他的
{
$(“#errormsg6”).html(“”);
用户_email2=item5;
如果(!emailformat.test(第5项))
{
$(“#errormsg6”).html(“错误的电子邮件格式”);
用户_email2=“”;
}
其他的
{
$(“#errormsg6”).html(“”);
用户_email2=item5;
$.ajax(
{
键入:“POST”,
url:'classes/validatelogin.php?f=1',
数据:“用户_email2=“+item5,
成功:功能(msg)
{
如果(消息==“确定”)
{
用户_emailajax2=“”;
$(“#errormsg6”).html(“电子邮件不存在”);
}
else if(msg==“存在”)
{
用户_emailajax2=项目5;
$(“#errormsg6”).html(“”);
}
}
});
}
}
}
/*--------------定义验证密码*/
var validate\u password\u login=函数()
{
var item5=$(“#user_email2”).val().toLowerCase();
var item6=$(“#用户_密码2”).val();
如果(项目6.length<8 | |项目6.length>20)
{
$(“#errormsg7”).html(“密码:8-20个字符”);
用户_password2=“”;
}
其他的
{
$(“#errormsg7”).html(“”);
用户密码2=item6;
如果(用户\u email2!=“”&用户\u emailajax2!=“”)
{
$.ajax(
{
方法:“张贴”,
url:“classes/validatelogin.php?f=2”,
数据:“user_email2=“+item5+”&user_password2=“+item6,
成功:功能(msg)
{
如果(消息==“错误消息”)
{
用户_passwordajax2=“”;
$(“#errormsg7”).html(“密码错误-请参阅忘记密码”);
}
否则如果(msg==“CorrectPw”)
{
用户\ u密码ajax2=item6;
$(“#errormsg7”).html(“”);
/*window.location.href=“管理属性”*/
}
}
});
}
}
}
/*--------------运行函数*/
$(“用户电子邮件2”)。在('focusout',验证电子邮件登录);
$(“#用户_密码2”)。在('focusout',验证#密码_登录);
/*---------提交时停止*/
$(“#登录”).mousedown(函数()
{
验证电子邮件登录();
验证密码登录();
如果(用户|email2==“”|用户|emailajax2==“”| |用户|密码2==“”| |用户|密码ajax2==“”)
{
$(“#errormsg8”).html(“请填写所有字段(正确)”;
控制台日志(“提交错误”);
返回false;
}
其他的
{
$(“#errormsg8”).html(“”);
控制台日志(“提交为真”);
返回true;
}
});
});
已尝试解决方案-问题是,当用户输入错误的事件时,这是正常的,但如果用户随后输入了正确的值,则提交第一次返回false,第二次返回true。。。它应该在第一次执行时返回true
<input type="button" name="login" id="login" value="Submit">
$( "#login" ).mousedown(function()
{
validate_email_login();
validate_password_login();
if (user_email2 == "" || user_emailajax2 == "" || user_password2 == "" || user_passwordajax2 == "")
{
$("#errormsg8").html("Please Fill All Fields (Correctly)");
console.log("submit false");
return false;
}
else
{
$("#errormsg8").html("");
console.log("submit true");
$('[name=loginform]').submit();
}
});
});
$(“#登录”).mousedown(函数()
{
验证电子邮件登录();
验证密码登录();
如果(用户|email2==“”|用户|emailajax2==“”| |用户|密码2==“”| |用户|密码ajax2==“”)
{
$(“#errormsg8”).html(“请填写所有字段(正确)”;
控制台日志(“提交错误”);
返回false;
}
其他的
{
$(“#errormsg8”).html(“”);
控制台日志(“提交为真”);
$('[name=loginform]')。提交();
}
});
});
不要使用type=“submit”按钮,只需使用一个普通按钮,例如
。然后,当您检查完这些值并满意它应该发送时,只需调用:
$('[name=loginform]').submit();
因为当前发生的情况是,当您单击按钮时表单会提交,因为您没有阻止该事件的发生
如果您想阻止表单提交,我建议您不要像上面提到的那样使用该按钮并自行启动提交,或者您也可以使用onsubmit=“someFunction()”如果不应该提交,则返回false;如果应该提交,则返回true。我想说,您的代码存在一些问题和一些错误做法。 我看到你在努力学习JS所以请原谅我
var emailInput = $("#user_email2");
function submitForm() {
var email = emailInput.val().toLowerCase();
...
}
$(document).ready(function() {
"use strict";
var emailInput = $("#user_email2"),
emailError = $("#errormsg6"),
passwordInput = $("#user_password2"),
passwordError = $("#errormsg7");
function required (value) {
if (value) {
return true;
} else {
return false;
}
//this is just to make the code clear you could use
//`return value ? true : false` or `return !!value`
}
$('form:eq(0)').on('submit', function (e) {
var valid = true,
email = emailInput.val(),
password = passwordInput.val();
e.preventDefault();
if ( !required(email) ) {
emailError.text('Email is required');
valid = false;
}
if ( !required(password) ) {
passwordError.text('Password is required');
valid = false;
}
if ( valid ) {
$.ajax({
method: "POST",
url: "login.php",
data: {
email: email,
password: password
}
}).done(function (data, textStatus, jqXHR) {
//redirect user to main page
}).fail(function (jqXHR, textStatus, errorThrown) {
//show the user the error
})
}
});
});