请试试这个:工作版本: http: //jsfiddle.net/Uummr/2/show/ 代码 http://jsfiddle.net/Uummr/2/
您所需要的只是一个允许的合法域名来源,其余的将像魅力一样发挥作用。在这种情况下,除了法律电子邮件之外,它不会接受任何其他电子邮件,即var emailLegalReg = /^([\w-\.]+@(?!gmail.com)(?!yahoo.com)(?!hotmail.com)(?!aol.com)([\w-]+\.)+[\w-]{2,4})?$/;
读这个:(我做了相反的事情)http://www.designchemical.com/blog/index.php/jquery/email-validation-using-jquery/
希望这有助于事业:)
代码
// prepare the form when the DOM is ready
$(document).ready(function() {
$('#btn-submit').click(function() {
$(".error").hide();
var hasError = false;
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
var emailLegalReg = /^([\w-\.]+@(?!gmail.com)(?!yahoo.com)(?!hotmail.com)(?!aol.com)([\w-]+\.)+[\w-]{2,4})?$/;
var emailaddressVal = $("#UserEmail").val();
if(emailaddressVal == '') {
$("#UserEmail").after('<span class="error">Please enter your email address.</span>');
hasError = true;
} else if(!emailReg.test(emailaddressVal)) {
$("#UserEmail").after('<span class="error">Enter a valid email address.</span>');
hasError = true;
} else if(emailLegalReg.test(emailaddressVal)) {
$("#UserEmail").after('<span class="error">No eamil apart from gmail, hotmail or yahoo is allowed.</span>');
hasError = true;
}
if(hasError == true) { return false; }
else {
$("#UserEmail").after('<span class="error">Email accepted.</span>');
return false;
}
});
});
工作图像