使用jQuery和正则expression式validation电子邮件地址

我不太确定如何做到这一点。 我需要使用正则expression式来validation电子邮件地址,如下所示:

[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[AZ]{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('&nbsp;<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); } } }); }); 

validation在哪里,expression是什么?

更新


 function isValidEmailAddress(emailAddress) { var pattern = /^([az\d!#$%&'*+\-\/=?^_`{|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+(\.[az\d!#$%&'*+\-\/=?^_`{|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+)*|"((([ \t]*\r\n)?[ \t]+)?([\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|\\[\x01-\x09\x0b\x0c\x0d-\x7f\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))*(([ \t]*\r\n)?[ \t]+)?")@(([az\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|[az\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF][az\d\-._~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]*[az\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])\.)+([az\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|[az\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF][az\d\-._~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]*[az\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])\.?$/i; return pattern.test(emailAddress); }; 

 if( !isValidEmailAddress( emailaddress ) ) { /* do stuff here */ } 
  • 注意: 请记住,没有100%的正则expression式电子邮件检查存在!

这是我的解决scheme:

 function isValidEmailAddress(emailAddress) { var pattern = new RegExp(/^[+a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i); // alert( pattern.test(emailAddress) ); return pattern.test(emailAddress); }; 

发现RegExp在这里: http : //mdskinner.com/code/email-regex-and-validation-jquery

 $(document).ready(function() { $('#emailid').focusout(function(){ $('#emailid').filter(function(){ var emil=$('#emailid').val(); var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/; if( !emailReg.test( emil ) ) { alert('Please enter valid email'); } else { alert('Thank you for your valid email'); } }) }); }); 

罗尔斯这好多了

  function isValidEmailAddress(emailAddress) { var pattern = new RegExp(/^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/); return pattern.test(emailAddress); }; 

我build议你使用Verimail.js 的jQuery插件 。

为什么?

  • IANA TLDvalidation
  • 语法validation(根据RFC 822)
  • 对最常见的TLD和电子邮件域进行拼写build议
  • 拒绝临时的电子邮件帐户域名,如mailinator.com

怎么样?

在你的网站上joinverimail.jquery.js并使用这个函数:

 $("input#email-address").verimail({ messageElement: "p#status-message" }); 

如果你有一个表单,并希望validation提交的电子邮件,你可以使用getVerimailStatus函数:

 if($("input#email-address").getVerimailStatus() < 0){ // Invalid email }else{ // Valid email } 

使用Javascript:

 var pattern = new RegExp("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"); var result = pattern .test(str); 

正则expression式不允许:

 abc@gmail..com abc@gmail.com.. 

允许:

 abc.efg@gmail.com abc@gmail.com.my 

资料来源: http : //www.mkyong.com/regular-expressions/10-java-regular-expression-examples-you-should-know/

我们还可以使用正则expression式(/^([\w.-]+)@([\w-]+)((.(w({2,3})+)$/i)来validation电子邮件地址格式正确与否。

 var emailRegex = new RegExp(/^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$/i); var valid = emailRegex.test(emailAddress); if (!valid) { alert("Invalid e-mail address"); return false; } else return true; 

尝试这个

 function isValidEmailAddress(emailAddress) { var pattern = new RegExp(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/); return pattern.test(emailAddress); };