Javascript multiple email regexp validation

不羁岁月 提交于 2019-11-28 13:29:57

Split the emails on a comma and validate the entries

var x = getEmails();
var emails = x.split(",");
emails.forEach(function (email) {
  validate(email.trim());
});

Where getEmails() gets the emails from the page, and validate runs your regex against the emails

try this

    function validateEmails(string) {
        var regex = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
        var result = string.replace(/\s/g, "").split(/,|;/);        
        for(var i = 0;i < result.length;i++) {
            if(!regex.test(result[i])) {
                return false;
            }
        }       
        return true;
    }

accept both comma and semicolon as separator

You should be able to split the entry by commas, and then test the individual email subentries against the regexp.

var valid = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
var entries = entry.split(",");

if(valid.test(entries[0]))...    //or however your testing against the regex

You might also want to trim any whitespace before testing any of the stripped email substrings.

Here is a regex for multiple mail validation without split

function validateMultipleEmails(string) {
    var regex = /^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-\.]+)+([;]([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-\.]+))*$/;
    return regex.test(string);
}

https://regex101.com/r/TUXLED/1

JAY

Try this for jquery validation:

jQuery.validator.addMethod("multiemail", function (value, element) {
    if (this.optional(element)) {
        return true;
    }
    var emails = value.split(','),
        valid = true;
    for (var i = 0, limit = emails.length; i < limit; i++) {
        value = emails[i];
        valid = valid && jQuery.validator.methods.email.call(this, value, element);
    }
    return valid;
}, "Invalid email format: please use a comma to separate multiple email addresses.");

function validateEmail(emailAddress) {
  var emailPattern = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  return emailPattern.test(emailAddress);
}

function validate() {
  $("#result").text("");
  var email = $("#emailAddress").val();
  if (validateEmail(email)) {
    $("#result").text(email + " validation successful");
    $("#result").css("color", "white");
  } else {
    $("#result").text(email + " validation failed");
    $("#result").css("color", "red");
  }
  return false;
}

$("form").bind("submit", validate);
.divSection{
  text-align: center; padding: 8%;
  }
.pSection{
  border: none;    color: white;    padding: 10px 100px;    text-align: center;    text-decoration: none;    display: inline-block;    font-size: 24px;    margin: 3% 0%;    border-radius: 6px;    -webkit-transition-duration: 0.4s;   transition-duration: 0.4s;    font-family: Roboto-Regular,Helvetica,Arial,sans-serif; background-color: #4184f3; margin: auto;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="divSection">
<form action="dashboard/user/profile" method="POST">
  <input id="emailAddress" placeholder="Enter Email" value="shubham20.yeole@gmail.com">
  <input type='submit' value="check">
</form>
  </div>

<div class="divSection" >
<p class="pSection" id='result'></p>
</div>

This is javascript code to check if email contains more than one @ characters

var count=0;
email = "shubham20.ye@ole@gmail.com";
alert(email);
for(i =0; i<email.length;i++){
if(email.charAt(i) == "@"){
count++;
}}
if(count>1){
   alert("not ok")
} else {
    alert("ok")
}

Alternate way is by using the standard pattern of the email

var pattern= /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
re.test(email);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!