I have to validate a email field which can contain multiple email address separated by (;). The following is the code i used
$("body").find(".reqEmail").filter(function(){
var regex = new RegExp(/^[_A-Za-z0-9-]+[^(),:;<>\\[\\]@]*@[^(),:;<>\\[\\]@]*(\\.[A-Za-z]{2,})+$/);///^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
var email=$(this).val()
if(regex.test(email)==false){
e.preventDefault();
$(this).css("border","solid 1px red");
$(this).parent().find("#ReceAppEmail").html("Invalid Email!!");
}
else{return true;}
});
It always give the error message, even i insert 1 email address. I cannot find where i went wrong. any suggestions?
FYI: This is included in the form submission ( onsubmit
)
You can grab all the email addresses separated by semicolons using the regex below:
/(?:((?:[\w-]+(?:\.[\w-]+)*)@(?:(?:[\w-]+\.)*\w[\w-]{0,66})\.(?:[a-z]{2,6}(?:\.[a-z]{2})?));*)/g
You can do this by below code.
function validatecommSeptEmail(commSeptEmail)
{
var regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,5}$/;
return (regex.test(commSeptEmail)) ? true : false;
}
function validateMultiplecommSeptEmails(emailcntl, seperator)
{
var value = emailcntl.value;
if (value != '') {
var result = value.split(seperator);
for (var i = 0; i < result.length; i++) {
if (result[i] != '') {
if (!validatecommSeptEmail(result[i])) {
emailcntl.focus();
alert('Please check, `' + result[i] + '` email addresses not valid!');
return false;
}
}
}
}
return true;
}
How to use it?
onblur="validateMultiplecommSeptEmails(this,',');"
Try this
function validateEmail(email) {
var regixExp = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
$(".reqEmail").css("border","solid 1px red");
$("#ReceAppEmail").html("Invalid Email!!");
return regixExp.test(email);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.