Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Validate multiple email id's using javascript

By , 28 Nov 2011
Rate this:
Please Sign up or sign in to vote.
I just used this on a website. Thanks.
But I made a change, using the ASP.NET CustomValidator and the ClientValidationFunction property.
 
<asp:customvalidator id="CustomValidator1" runat="server" clientvalidationfunction="validateMultipleEmailsCommaSeparated" xmlns:asp="#unknown">
 ErrorMessage="Not valid" ControlToValidate="toBox">
 
and the function updated to this:
 
function validateEmail(field) {
        var regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,5}$/;
        return (regex.test(field)) ? true : false;
    }
    function validateMultipleEmailsCommaSeparated(source, args) {
        var text = args.Value;
        if (text != '') {
            var result = text.split(",");
            for (var i = 0; i < result.length; i++) {
                var trimmed = result[i].trim();
                if (trimmed != '') {
                    if (!validateEmail(trimmed)) {
                        alert('Please check, `' + trimmed + '` email addresses not valid!');
                        return false;
                    }
                }
            }
        }
        return true;
    }
 
Since the ASP.NET ClientValidationFunction has a predefined set of parameters (source and arguments), I only made a small change and placed the separator right into the code. This also saved me from touching the onblur client event by myself.
 
Also, used the trim() to remove spaces before or after the email, that was causing problems if the users type the email addresses with a space after the comma.
Thanks.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Leonardo Paneque
Team Leader
United States United States
Leonardo loves to code with C# and thinks .NET platforms rocks.
He has a Master degree in Computer Sciences and likes to share code and ideas.
Follow on   Twitter

Comments and Discussions

 
QuestionValidate multiple email id's using javascript PinmemberGiven Mbatsane7-Sep-12 2:58 
Thanks dude, that really helped me alot after i took me for a ride

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 28 Nov 2011
Article Copyright 2011 by Leonardo Paneque
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid