使用正则表达式 jquery 验证电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9572254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
validate email with regex jquery
提问by hyperrjas
referring to this issue:
提到这个问题:
how can I set a minimum length for a field with jquery?
<form id="new_invitation" class="new_invitation" method="post" data-remote="true" action="/invitations" accept-charset="UTF-8">
<div id="invitation_form_recipients">
<input type="text" value="" name="invitation[recipients][]" id="invitation_recipients_0"><br>
<input type="text" value="" name="invitation[recipients][]" id="invitation_recipients_1"><br>
<input type="text" value="" name="invitation[recipients][]" id="invitation_recipients_2"><br>
<input type="text" value="" name="invitation[recipients][]" id="invitation_recipients_3"><br>
</div>
<input type="submit" value="Send invitation" name="commit">
</form>
Code for set a minimum length for a field with jquery?
使用jquery为字段设置最小长度的代码?
$('#new_invitation').submit(function(event) {
if ($('#invitation_form_recipients input').filter(function() {
return $(this).val();
}).length == 0) {
// all the fields are empty
// show error message here
// this blocks the form from submitting
event.preventDefault();
}
});
How can I validate that every field input have a valid email address with jquery? in the above code?
如何使用 jquery 验证每个字段输入是否具有有效的电子邮件地址?在上面的代码中?
Thank you!
谢谢!
The Solution!
解决方案!
$('#new_invitation').submit(function(e) {
$('#invitation_form_recipients input').each(function(e) {
email_address = $(this);
email_regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i;
if(!email_regex.test(email_address.val())){ alert('this is not valid email'); e.preventDefault(); return false; }
});
});
回答by rjz
You probably want to use a regex like the one described hereto check the format. When the form's submitted, run the following test on each field:
您可能想使用此处描述的正则表达式来检查格式。提交表单后,对每个字段运行以下测试:
var userinput = $(this).val();
var pattern = /^\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b$/i
if(!pattern.test(userinput))
{
alert('not a valid e-mail address');
}?
回答by Aditi_Systematix
This regex can help you to check your email-address according to all the criteria which gmail.com used .
这个正则表达式可以帮助您根据 gmail.com 使用的所有标准检查您的电子邮件地址。
var re = /^\w+([-+.'][^\s]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
var emailFormat = re.test($("#email").val());// this return result in boolean type
if (emailFormat) {}
回答by gaurav ghai
In javascript you can easily validate email by using many javascript function such as onkeyup() onchange() and many more! After calling a function use RegExp of javascript to validate email as shown below.
在 javascript 中,您可以使用许多 javascript 函数(例如 onkeyup() onchange() 等等)轻松验证电子邮件!调用函数后,使用 javascript 的 RegExp 来验证电子邮件,如下所示。
RegExp(/^(("[\w-\s]+")|([\w-]+(?:.[\w-]+))|("[\w-\s]+")([\w-]+(?:.[\w-]+)))(@((?:[\w-]+.)*\w[\w-]{0,66}).([a-z]{2,6}(?:.[a-z]{2})?)$)|(@[?((25[0-5].|2[0-4][0-9].|1[0-9]{2}.|[0-9]{1,2}.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2}).){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})]?$)/i);
RegExp(/^(("[\w-\s]+")|([\w-]+(?:.[\w-]+) )|("[\w-\s]+") ([\w-]+(?:.[\w-]+)))(@(((?:[\w-]+.)*\w[\w-]{0,66}).( [az]{2,6}(?:.[az]{2})?)$)|(@[?((25[0-5].|2[0-4][0-9]. |1[0-9]{2}.|[0-9]{1,2}.))((25[0-5]|2[0-4][0-9]|1[0- 9]{2}|[0-9]{1,2}).){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2 }|[0-9]{1,2})]?$)/i);
Above syntax is used to validate email in javascript. For detailed explanation check out https://www.htmlhints.com/article/10/email-validation-in-javascript-using-regular-expression-with-match
以上语法用于在 javascript 中验证电子邮件。有关详细说明,请查看https://www.htmlhints.com/article/10/email-validation-in-javascript-using-regular-expression-with-match
回答by ARC
Email: {
group: '.col-sm-3',
enabled: false,
validators: {
//emailAddress: {
// message: 'Email not Valid'
//},
regexp: {
regexp: '^[^@\s]+@([^@\s]+\.)+[^@\s]+$',
message: 'Email not Valid'
},
}
},
回答by Sakthivel
function mailValidation(val) {
var expr = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
if (!expr.test(val)) {
$('#errEmail').text('Please enter valid email.');
}
else {
$('#errEmail').hide();
}
}