Click here to Skip to main content
11,502,821 members (60,024 online)
Click here to Skip to main content

Tagged as

Validate multiple email id's using javascript

, 28 Nov 2011 CPOL 5.1K 1
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....
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)

Share

About the Author

Leonardo Paneque
Architect
United States United States
Leonardo loves to code with C# on any platform and OS.
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 Pin
Given Mbatsane7-Sep-12 2:58
memberGiven Mbatsane7-Sep-12 2:58 

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 | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 28 Nov 2011
Article Copyright 2011 by Leonardo Paneque
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid