Sunday, June 22, 2014

Regular expression to validate multiple Email IDs separated by comma

Today I am giving you a simple regular expression to validate multiple email IDs separated by comma. This is one of the very common expression we require during the application development.
Regular expression you may use for this purpose is,
^\s*((\s*[a-zA-Z0-9\._%-]+@[a-zA-Z0-9\.-]+\.[a-zA-Z]{2,4}\s*[,]{1}\s*){1,100}?)?([a-zA-Z0-9\._%-]+@[a-zA-Z0-9\.-]+\.[a-zA-Z]{2,4})\s*$
Below is the complete code for a sample asp.net page to validate multiple Email IDs
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MultipleEmailID.aspx.cs" Inherits="DotnetGalaxy.MultipleEmailID" %>
 
<!DOCTYPE html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="txtEmailID" runat="server" Width="400px"></asp:TextBox>
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" />
        <br />
        <asp:RegularExpressionValidator ID="REEmailID" runat="server" ControlToValidate="txtEmailID" ErrorMessage="Please enter valid email addresses separated by commas and in the following format xxxx@xxxx.com" Font-Bold="True" ForeColor="Red" ValidationExpression="^\s*((\s*[a-zA-Z0-9\._%-]+@[a-zA-Z0-9\.-]+\.[a-zA-Z]{2,4}\s*[,]{1}\s*){1,100}?)?([a-zA-Z0-9\._%-]+@[a-zA-Z0-9\.-]+\.[a-zA-Z]{2,4})\s*$"></asp:RegularExpressionValidator>
    
    </div>
    </form>
</body>
</html>

And the output will be something like below,
Valid One:
image
Invalid One:
image

1 comment:

Neil said...

how to use in mvc