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
)
Email validation in jQuery using regular expression is most important when you get real email id. We will create form and click button to validate email id. If email is invalid then display error message, otherwise display success message using regex in jQuery.
You can also use jQuery validate plugin, which enables to do email validation of a textbox if implemented with form We would require extra JS plugin to be used with jQuery validate for email validation, you can include it in your page using this link So the sample to validate form and email would look like this
Another simple solution would be here to use HTML 5 "pattern" attribute to validate any email. In the above HTML form, we have included pattern attribute, which has email pattern similar to regex solution one and then we can validate email address using it. You can modify the pattern based on your need.
a regex alone on clientside, does not know if there is a mailserver nor if the domain itself exists. it merly checks if the syntax of any email is valid or not. Its only to help the user writing the correct address. its not a validation.
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
http://regexr.com/3b6al
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,',');"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With