javascript - How to validate email addresses in a textarea - Stack Overflow

I want to validate a textarea for email and also I want that same textarea to allow a number of emails.

I want to validate a textarea for email and also I want that same textarea to allow a number of emails. I need this for a refer a friend option.

HTML part:

<textarea class="span12" id="demo"></textarea>

I want to validate a textarea for email and also I want that same textarea to allow a number of emails. I need this for a refer a friend option.

HTML part:

<textarea class="span12" id="demo"></textarea>
Share edited Mar 14, 2014 at 8:05 hberg 5351 gold badge3 silver badges20 bronze badges asked Mar 14, 2014 at 7:30 user3408415user3408415 11 silver badge5 bronze badges 1
  • what where is the rest? did you even try anything? html should be final part :P – Jorge Y. C. Rodriguez Commented Mar 14, 2014 at 7:35
Add a ment  | 

6 Answers 6

Reset to default 2

try this

function ValidateEmails() {  
  var emailList= $("#demo").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})+$/;
       return regex.test(emailList[i]);
    }
}

you will need to enter a seperated emailids in your text area

function validateEmail(field) {
    var regex=/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i;
    return (regex.test(field)) ? true : false;
}
function validateMultipleEmailsCommaSeparated(value) {
    var result = value.split(",");
    for(var i = 0;i < result.length;i++)
       if(!validateEmail(result[i])) 
           return false;
    return true;
}

This will help you... with some of example... [email protected],[email protected]

Your question is really two questions: 1. How do I validate an email from an input field? 2. How do I validate multiple emails from an input field?

For #1, I'd suggest using the tried and true jQuery Validation library instead of rolling your own email validator. Docs here: http://jqueryvalidation/email-method

After including the jQuery Validation library, you'd call it like this:

$( "#demo" ).validate({ rules: { field: { required: false, email: true } } });

Of course that'll only work for a single email address.

To answer #2, you'll have to split the value of the textarea by some delimiter (e.g. whitespace, mas, etc) and then validate each string separately.

Here's one way to validate email addresses separated by ma (,).

There's a submit and reset button, a textarea for taking the mails, and function validateform to validate the emails in the textarea.

<html>
<head>
        <script src="https://ajax.googleapis./ajax/libs/jquery/1.12.4/jquery.min.js"></script><html>
<style>
</style>
</head>
<body>
 <div class="input">

   <p class="label"><h2>*Email Address:</h2>           
    <textarea placeholder="Enter ur mails by separator (,) " cols="30" rows="10" id="email_val" name="mail"></textarea>
   </p>  
<p id="demo"></p>
   <p class="label"><input type="submit" onclick="ValidateForm()"></p>
    <p class="label"><input type="reset" onclick="history.go(0)"></p>

 </div>
 <script>

 function ValidateForm() {
                var emailList = $("#email_val").val().split(',');
                for (i = 0; i < emailList.length; i++)
                {
                    for (i = 0; i < emailList.length; i++) {
                       // var expr =/^(([a-zA-Z]{1})|([a-zA-Z]{1}[a-zA-Z]{1})|([a-zA-Z]{1}[0-9]{1})|([0-9]{1}[a-zA-Z]{1})|([a-zA-Z0-9][a-zA-Z0-9-_]{1,61}[a-zA-Z0-9]))\.([a-zA-Z]{2,6}|[a-zA-Z0-9-]{2,30}\.[a-zA-Z]{2,6})$/;
                        var expr = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
                        var result = expr.test(emailList[i]);
                        if (!result) {
                              document.getElementById("demo").style.color="red";
                            document.getElementById("demo").innerHTML = "sorry enter a valid email adrress";

                            return false;

                        }
                    }
                           document.getElementById("demo").style.color="blue";
                           if(emailList.length==1)
{                          
                        document.getElementById("demo").innerHTML = "Ur email address is successfully submitted";
                        return true;
 }    else{
   document.getElementById("demo").innerHTML = "Ur "+emailList.length+" email addresses are successfully submitted";

} 

                    }



                }

 </script>
</body>
</html>

*

There is too much different code for email validation - just look around:

Email validation using jQuery

Also, it is a bad idea to make textarea with emails. You should watch for separator, spaces, etc... Better to make a simple input, add emails to script array and then display as removable buttons.

Try this:

$.validator.addMethod('demo', function(value, element) {
    if (this.optional(element)) return true;

    var flag = true;
    var addresses = value.replace(/\s/g,'').split(',');
    for (i = 0; i < addresses.length; i++) {
        flag = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(addresses[i]);
    }
    return flag;
}, '');

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745056995a4608726.html

相关推荐

  • javascript - How to validate email addresses in a textarea - Stack Overflow

    I want to validate a textarea for email and also I want that same textarea to allow a number of emails.

    20小时前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信