-1
<div class="fluid field">
    <label>e-mail Ids:</label>
    <textarea rows="1" style="height:100px" id="mailids" value="<%- mailids %>"></textarea>
 </div>

below is my JS code which i tried

'change #mailids': function (e) {
        var strVale = $(e.target).val();
        if (strVale.substring(strVale.length - 1) == ";") {
            strvalue = strVale.substring(0, strVale.length - 1);
        }
        var arr = strVale.split(';');
        var isValid = true;
        var self = this;

        arr.forEach(function (item) {
            var x = item;
            var atpos = x.indexOf("@");
            var dotpos = x.lastIndexOf(".");
            if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= x.length) {
                isValid = false;
            }
        });
      }

I need to validated the two email id separated by ;(semi colon) if its not valid email it should show error message

below input also accepts as valid

image

chethu
  • 386
  • 1
  • 3
  • 26
  • Have you tried regex? – Shubham May 10 '19 at 08:39
  • What is the issue with your current code? How exactly does it fail? –  May 10 '19 at 08:40
  • using regex i can validated only one email after validating i need to separate it by ; – chethu May 10 '19 at 08:40
  • @ChrisG the issue is even if i give `colon (-)` also it will accept as valid email id other than `;` it should not accept anything – chethu May 10 '19 at 08:41
  • Why do you have value="" on the textarea? It should be `` – mplungjan May 10 '19 at 08:43
  • So if I understand you correctly, your issue *isn't* `How to validate two email Id separated by ; using jquery/ javascript`, but your issue is `How to validate email`? –  May 10 '19 at 08:44
  • @ChrisG No not to validate email if user gives multiple email it should be separated by ; – chethu May 10 '19 at 08:46
  • @mplungjan that is to for another purpose – chethu May 10 '19 at 08:47
  • Please add examples of input that fail your test. –  May 10 '19 at 08:49
  • @ChrisG see the image which i updated in the question – chethu May 10 '19 at 08:53
  • If you use a suitable regex for the address check, that input will fail because it is treated as a single address and thus won't pass the regex check. –  May 10 '19 at 08:55
  • Your issue is almost impossible. Instead have one email per field. If they need more, then have them add a field. – mplungjan May 10 '19 at 11:13

2 Answers2

0

Split the email is based on the semicolon. And for each email id, use a regular expression to validate email.

function validateEmail(email) {
    var re = /^(([^<>()\[\]\\.,;:\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 re.test(String(email).toLowerCase());
}

For regular expression based validation. See this answer.

ScrapCode
  • 2,109
  • 5
  • 24
  • 44
0

Just split email id's by comma or semicolon after that validate them

this will help you

 var x = getEmails();
var emails = x.split(",");
emails.forEach(function (email) {
  validate(email.trim());
});
Ankit Soni
  • 19
  • 2