2

I am trying to validate email ids in a textarea but i am stuck at a point as i want to validate each and every mail and also user may enters more mail ids using comma (,) separator for every mail id I need to validate at @ and dot (.) symbols and also com see my html and js codes get me out of this

<html>
  <head>
      <style>
      #email_val {
          height: 100px;
          width: 250px;
      }
      </style>
  </head>
<body>
<div class="input">
  <form name="myForm"  onsubmit="return ValidateForm();" method="post">
    <p class="label">
    <h2>*Email Address:</h2>
    <textarea placeholder="Enter ur mails byseparator (,) " id="email_val" name="mail"></textarea>
    </p>
    <p class="label">
      <input type="submit">
    </p>
    <p class="label">
      <input type="reset">
    </p>
  </form>
</div>
<p id="demo"></p>
<script type="text/javascript">
    function ValidateForm() {     
      var emailList= $("#email_val").val().split(',');
        for (i=0;i<emailList.length;i++)
        {
           var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
           var result[i]= regex.test(emailList[i]);
    if(result[i]==true)
    {
    document.getElementById("demo").innerHTML="Ur email address is successfully submitted";
    }else
    {
    document.getElementById("demo").innerHTML="sorry enter a valid"; 
    }  
    }
    </script>
</body>
</html>
Sanjeev Kumar
  • 3,113
  • 5
  • 36
  • 77
  • 1
    result[i] will not work. Define result first : result=[] – Jonas Wilms Sep 02 '16 at 05:59
  • Use return true add the end, and return false in the if(wrong) – Jonas Wilms Sep 02 '16 at 06:01
  • it also not working i defined var result=[]; and then result[i]=regex.tex() – shyam sanju Sep 02 '16 at 06:03
  • 1
    Add another `}` before `` – Prateek Gupta Sep 02 '16 at 06:05
  • Possible duplicate of [Validate email address in JavaScript?](http://stackoverflow.com/questions/46155/validate-email-address-in-javascript) – HudsonPH Sep 02 '16 at 06:16
  • If you are using regex to validate emails, use a very permissiv one. Yours would fail abc@åre.se which is a perfectly valid email, as is "Hello World"@123.123.123.123 (including the quotes). Emails allow for a lot more characters than most imagine and any time you encounter an email from another country you are bound to get in trouble with most regex examples. The easiest is to just check that you have an @ char with something on both sides and then actually try to send an email with a confirmation link in it. – David Mårtensson Sep 02 '16 at 06:21
  • I believe that even a@123456 might qualify as an valid email as long as the numbers represent the integer form of an IP – David Mårtensson Sep 02 '16 at 06:23

3 Answers3

0
function ValidateForm() { 
var emailList= $("#email_val").val().split(','); 
for (i=0;i<emailList.length;i++) { 
var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/; 
var result= regex.test(emailList[i]);
 if(result==false) { 
document.getElementById("demo").innerHTML="Error";
return false;
}
}
document.getElementById("demo").innerHTML="ok"; 
return true;
}

The onsubmit eventListener stops submitting the form if you return false

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
0

function ValidateForm() {

                var emailList = $("#email_val").val().split(',');
                for (i = 0; i < emailList.length; i++)
                {
                    for (i = 0; i < emailList.length; i++) {
                        var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
                        var result = regex.test(emailList[i]);
                        if (!result) {
                            document.getElementById("demo").innerHTML = "sorry enter a valid";
                            return false;

                        }

                    }

                        document.getElementById("demo").innerHTML = "Ur email address is successfully submitted";
                        return true;
                    }
                }
<html>
<head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script><html>

<style>

#email_val{

height:100px;

width:250px;

}

</style>
</head>
<body>

 <div class="input">
    <form name="myForm"  onsubmit="return ValidateForm();" method="post">


   <p class="label"><h2>*Email Address:</h2>           
    <textarea placeholder="Enter ur mails byseparator (,) " id="email_val" name="mail"></textarea>
   </p>  
<p id="demo"></p>

   <p class="label"><input type="submit"></p>

   <p class="label"><input type="reset"></p>
    </form>
 </div>
</body>
</html>
function ValidateForm() {

                var emailList = $("#email_val").val().split(',');
                for (i = 0; i < emailList.length; i++)
                {
                    for (i = 0; i < emailList.length; i++) {
                        var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
                        var result = regex.test(emailList[i]);
                        if (!result) {
                            document.getElementById("demo").innerHTML = "sorry enter a valid";
                            return false;

                        }

                    }

                        document.getElementById("demo").innerHTML = "Ur email address is successfully submitted";
                        return true;
                    }
                }
Nitya Kumar
  • 967
  • 8
  • 14
0

This works for me

var emailList= $("#email_val").val().split(',');
var flag = 1; 
for (i=0;i<emailList.length;i++){
    if(validateEmail(emailList[i])==false){
        flag=0;
        break;
    }
}

if(flag==0){
    document.getElementById("demo").innerHTML="Error";
}
else{
    document.getElementById("demo").innerHTML="Ok";
}

function validateEmail(email) {            
    var re = /\S+@\S+\.\S+/;
    return re.test(email);
}