使用 jQuery 和正则表达式验证电子邮件地址
我不太确定该怎么做。我需要使用正则表达式来验证电子邮件地址,如下所示:
[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)
然后我需要在 jQuery 函数中运行它,如下所示:
$j("#fld_emailaddress").live('change',function() {
var emailaddress = $j("#fld_emailaddress").val();
// validation here?
if(emailaddress){}
// end validation
$j.ajax({
type: "POST",
url: "../ff-admin/ff-register/ff-user-check.php",
data: "fld_emailaddress="+ emailaddress,
success: function(msg)
{
if(msg == 'OK') {
$j("#fld_username").attr('disabled',false);
$j("#fld_password").attr('disabled',false);
$j("#cmd_register_submit").attr('disabled',false);
$j("#fld_emailaddress").removeClass('object_error'); // if necessary
$j("#fld_emailaddress").addClass("object_ok");
$j('#email_ac').html(' <img src="img/cool.png" align="absmiddle"> <font color="Green"> Your email <strong>'+ emailaddress+'</strong> is OK.</font> ');
} else {
$j("#fld_username").attr('disabled',true);
$j("#fld_password").attr('disabled',true);
$j("#cmd_register_submit").attr('disabled',true);
$j("#fld_emailaddress").removeClass('object_ok'); // if necessary
$j("#fld_emailaddress").addClass("object_error");
$j('#email_ac').html(msg);
}
}
});
});
验证在哪里以及表达式是什么?
I'm not too sure how to do this. I need to validate email addresses using regex with something like this:
[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)
Then I need to run this in a jQuery function like this:
$j("#fld_emailaddress").live('change',function() {
var emailaddress = $j("#fld_emailaddress").val();
// validation here?
if(emailaddress){}
// end validation
$j.ajax({
type: "POST",
url: "../ff-admin/ff-register/ff-user-check.php",
data: "fld_emailaddress="+ emailaddress,
success: function(msg)
{
if(msg == 'OK') {
$j("#fld_username").attr('disabled',false);
$j("#fld_password").attr('disabled',false);
$j("#cmd_register_submit").attr('disabled',false);
$j("#fld_emailaddress").removeClass('object_error'); // if necessary
$j("#fld_emailaddress").addClass("object_ok");
$j('#email_ac').html(' <img src="img/cool.png" align="absmiddle"> <font color="Green"> Your email <strong>'+ emailaddress+'</strong> is OK.</font> ');
} else {
$j("#fld_username").attr('disabled',true);
$j("#fld_password").attr('disabled',true);
$j("#cmd_register_submit").attr('disabled',true);
$j("#fld_emailaddress").removeClass('object_ok'); // if necessary
$j("#fld_emailaddress").addClass("object_error");
$j('#email_ac').html(msg);
}
}
});
});
Where does the validation go and what is the expression?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
更新
UPDATES
这是我的解决方案:
在这里发现正则表达式:http://mdskinner .com/code/email-regex-and-validation-jqueryThis is my solution:
Found that RegExp over here: http://mdskinner.com/code/email-regex-and-validation-jqueryLZ这好多了
Lolz this is much better
我建议您使用 jQuery 插件 Verimail.js。
为什么?
如何?
包括verimail.jquery.js 在您的网站上并使用该功能:
如果您有一个表单并希望在提交时验证电子邮件,您可以使用 getVerimailStatus 函数:
I would recommend that you use the jQuery plugin for Verimail.js.
Why?
How?
Include verimail.jquery.js on your site and use the function:
If you have a form and want to validate the email on submit, you can use the getVerimailStatus-function:
JavaScript:
正则表达式不允许用于:
允许:
来源:http:// /www.mkyong.com/regular-expressions/10-java-regular-expression-examples-you-should-know/
Javascript:
The regex is not allowed for:
Allowed for:
Source: http://www.mkyong.com/regular-expressions/10-java-regular-expression-examples-you-should-know/
我们还可以使用正则表达式 (/^([\w.-]+)@([\w-]+)((.(\w){2,3})+)$/i) 来验证电子邮件地址格式正确与否。
We can also use regular expression (/^([\w.-]+)@([\w-]+)((.(\w){2,3})+)$/i) to validate email address format is correct or not.
试试这个
Try this
你可以使用这个功能
you can use this function
原生方法:
来源:https://jqueryvalidation.org/jQuery.validator.methods/
Native method:
Source: https://jqueryvalidation.org/jQuery.validator.methods/