gpt4 book ai didi

jquery - 检查联系表单中电子邮件地址的格式

转载 作者:行者123 更新时间:2023-12-03 22:55:20 24 4
gpt4 key购买 nike

我使用带有姓名、电子邮件和评论字段的简单表单从网页发送消息。还有一个隐藏的标题字段,该字段应该为空,以便提交表单 - 如果您愿意,可以进行垃圾邮件防护。

我在提交之前运行表单的 JQuery 代码工作正常,但目前仅在电子邮件地址字段中查找“@”字符。我想要的是更好地检查电子邮件地址格式是否正确。

这是代码。

    $(function() {  
$('.error').hide();
$(".button").click(function() {
// validate and process form here

$('.error').hide();
var name = $("input#name").val();
if (name == "") {
$("label#name_error").show();
$("input#name").focus();
return false;
}
var email = $("input#email").val();
if (!(email.indexOf('@') > 0)) {
$("label#email2_error").show();
$("input#email").focus();
return false;
}
var message = $("textarea#message").val();
if (message == "") {
$("label#message_error").show();
$("textarea#message").focus();
return false;
}
var title = $("input#title").val()
if(title !== "") {
$("input#title").focus();
return false;
}

var dataString = 'name='+ name + '&email=' + email + '&message=' + message;
//alert (dataString);return false;
$.ajax({
type: "POST",
url: "sendmail.php",
data: dataString,
success: function() {
$('#message_form').html("<div id='response'></div>");
$('#response').html("<div id='content_success_block' class='shadow_box'>")
.append("<div id='success_image'><img src='assets/misc/success.png'></div>")
.append("<div id='success_text'>Thanks for contacting us! We will be in touch soon.</div>")
.append("</div>")
.hide()
.fadeIn(2500, function() {
$('#response');
});
}
});
return false;
});

});

感谢任何帮助。

最佳答案

最适合“我”的方法

function validEmail(v) {
var r = new RegExp("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?");
return (v.match(r) == null) ? false : true;
}
<小时/>

¡重要!

你真的应该阅读THIS 。它提供了有关电子邮件正则表达式的大量信息,以及为什么没有真正好的“全部,结束全部”解决方案。简而言之,您必须确定什么最适合您的预期用户。

<小时/>

Incorperated

}  
var email = $("input#email").val();
if (!validEmail(email)) {
$("label#email2_error").show();
$("input#email").focus();
return false;
}

Alternate Strategy you might try: I tend to check email on keypress timeout (thus allowing the ability to fade out submit button if not all fields are ready)

关于jquery - 检查联系表单中电子邮件地址的格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10178268/

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