gpt4 book ai didi

javascript - 使用 JQuery Ajax 进行电子邮件验证

转载 作者:可可西里 更新时间:2023-10-31 22:55:25 25 4
gpt4 key购买 nike

我一直很难尝试使用 ajax 验证电子邮件使用 PHP 和 Javascript。我想做的是检查电子邮件是否已经存在于数据库中,如果存在,显示一条消息然后去返回表单,否则显示成功消息并关闭形式。问题是,如果电子邮件存在,它会显示“错误消息”,然后显示成功消息。我检查过类似的问题并尝试了其中的代码,但没有一个适用正确地符合我的要求。谢谢。

这是我得到的:

// JavaScript Document
$('#btnSubmit').click(function(){
if( validateForm() ){
// code to submit all the info, display the success message and close the form
}
else return false;
});


// I created a function to validate all the fields in the form
function validateForm() {
var firstName = $('#txtFirstName').val();
var lastName = $('#txtLastName').val();
var email = $('#txtMail').val();

// to validate firstName and lastName I do it like this:
if(!firstName || firstName.length === 0) {
message = "All fields are mandatory\\nFirst Name is required";
messageDialog("Warning", message, "warning", 2);
return false;
}
if( !firstName.match(letters) ) {
message = "Invalid name";
messageDialog("Warning", message, "warning", 2);
return false;
}
// So far, no problem at all. But when I tried to validate email:
else {
// use ajax to check if a user has been previously registered
// using this email
$.ajax({
url:"check_user.php", // url that will use
data:{ // data that will be sent
email:email
},
type:"POST", // type of submision
dataType:"text", // what type of data we'll get back
success:function(data)
{
// if check_user returns a number different than 0
// means that there's already a user registered with that email
if(data > 0 ) {
message = "This email is registered already!";
messageDialog("Error", message, "error", 2);
return false;
}
else { return true;}
}
});

}

// if everything is right then return true so the submit function continue its execution
return true;
}

最佳答案

您执行一个 ajax 请求,默认情况下该请求是异步的,这意味着它不会在执行其余功能之前等待您的请求响应。

因此,在您的情况下,一旦发出请求,它将返回 true,如函数结束时所述。然后,当您的 ajax 请求的响应返回时,它将触发您的 success 函数,该函数将显示错误消息。

你可以做类似的事情,使用 $.ajax 函数的 async 参数来确保它在执行任何操作之前等待你的请求的响应进一步。

function validateForm() {
var firstName = $('#txtFirstName').val();
var lastName = $('#txtLastName').val();
var email = $('#txtMail').val();

// to validate firstName and lastName I do it like this:
if(!firstName || firstName.length === 0) {
message = "All fields are mandatory\\nFirst Name is required";
messageDialog("Warning", message, "warning", 2);
return false;
}
if( !firstName.match(letters) ) {
message = "Invalid name";
messageDialog("Warning", message, "warning", 2);
return false;
}
// So far, no problem at all. But when I tried to validate email:
else {
// use ajax to check if a user has been previously registered
// using this email
var valid = false;
$.ajax({
url:"check_user.php",
async: false,
data:{ // data that will be sent
email:email
},
type:"POST", // type of submision
dataType:"text", // what type of data we'll get back
success:function(data)
{
// if check_user returns a number different than 0
// means that there's already a user registered with that email
if(data == 0 ) {
valid = true;
}
}
});
if(!valid) {
message = "This email is registered already!";
messageDialog("Error", message, "error", 2);
return false;
} else { return true; }
}
}

关于javascript - 使用 JQuery Ajax 进行电子邮件验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20381476/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com