Click here to Skip to main content
15,881,649 members
Articles / Web Development / ASP.NET
Tip/Trick

Validate multiple email id's using javascript

Rate me:
Please Sign up or sign in to vote.
4.67/5 (2 votes)
23 Nov 2011CPOL 51.1K   4   2
Entered multiple email id's with comma/semicolon separator in text box to validate using javascript
JavaScript
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(emailcntl, seperator) {
    var value = emailcntl.value;
    if (value != '') {
        var result = value.split(seperator);
        for (var i = 0; i < result.length; i++) {
            if (result[i] != '') {
                if (!validateEmail(result[i])) {
                    emailcntl.focus();
                    alert('Please check, `' + result[i] + '` email addresses not valid!');
                    return false;
                }
            }
        }
    }
    return true;
}



Use javascript in text box lost focus event
Using semicolon separator

ASP.NET
<asp:TextBox onblur="validateMultipleEmailsCommaSeparated(this,';');" ID="txtToEmail" Text="" runat="server" /> 

License

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


Written By
Web Developer Mahindra Logisoft Business Solution Limited, Chenn
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralReason for my vote of 5 good hint. Pin
Leonardo Paneque23-Nov-11 17:42
Leonardo Paneque23-Nov-11 17:42 
GeneralRe: Thanks a ton Buddy Pin
Ramanujam Shankar9-Feb-12 20:57
Ramanujam Shankar9-Feb-12 20:57 

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

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