Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Validate multiple email id's using javascript

4.67/5 (2 votes)
23 Nov 2011CPOL 51.7K  
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)