Validating email in jQuery with RegExp

I need to check an email field, which can contain multiple email addresses separated by (;). Below 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 gives an error message, even I insert 1 email address. I can not find where I was wrong. any suggestions?
FYI: This is included in the submission form ( onsubmit )

+6
source share
3 answers

You can capture all email addresses separated by semicolons using the regex below:

 /(?:((?:[\w-]+(?:\.[\w-]+)*)@(?:(?:[\w-]+\.)*\w[\w-]{0,66})\.(?:[az]{2,6}(?:\.[az]{2})?));*)/g 

http://regexr.com/3b6al

+2
source

You can do this using the code below.

 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,',');" 
+1
source

try it

 function validateEmail(email) { var regixExp = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([az]{2,6}(?:\.[az]{2})?)$/i; $(".reqEmail").css("border","solid 1px red"); $("#ReceAppEmail").html("Invalid Email!!"); return regixExp.test(email); } 
0
source

Source: https://habr.com/ru/post/988886/


All Articles