gpt4 book ai didi

javascript - AJAX 之前的 Javascript 中的电子邮件验证

转载 作者:行者123 更新时间:2023-11-28 19:00:15 26 4
gpt4 key购买 nike

所以我得到了用于表单提交的js代码,一切正常,但是,我不知道在代码中的位置和内容写入,以便电子邮件得到验证检查。我应该在电子邮件中写什么以及在哪里进行验证检查?

$(document).ready(function() {
$("#submit").click(function() {

var name = $("#fullname2").val();
var email = $("#fullemail2").val();
var state = $("#selectstate").val();
// Returns successful data submission message when the entered information is stored in database.
var dataString = 'FullName=' + name + '&email=' + email + '&SovereignState=' + state;
if (name == '' || email == '' || state == '') {
$('#required_fields').show();
} else {
// AJAX Code To Submit Form.
$.ajax({
type: "POST",
url: "demo.php",
data: dataString,
cache: false,
success: function(phpSays) {
if (phpSays == "OK") {
$('#email_error').show();
$('#required_fields').hide();
} else {
$('#sinatra2').hide();
$('#thanks').fadeIn(1000);
$('#spreading_message').delay(1800).fadeIn(1500);
$('#by_social').delay(3000).fadeIn(1500);
$('#email_error').hide();
$('#required_fields').hide();
}
}
});
}
return false;
});
});

最佳答案

查看您的代码,我可以建议使用以下方法来说明您可以在哪里进行电子邮件验证

if(name==''||email==''||state=='')
{
$('#required_fields').show();
}//this is fine
else if(!valid(email))//call a function which validates email and returns true or false
{
//Display the message either with same $('#required_fields') changing the text or
//Add one more field to display invalid email message
}
else
{
//Your ajax call here
}

现在您的有效函数将如下所示

function valid(email)
{
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
return emailReg.test(email); //this will either return true or false based on validation
}

关于javascript - AJAX 之前的 Javascript 中的电子邮件验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32730764/

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