Thursday, June 23, 2016

How to validate corporate or business email address in asp.net

In this article I am going to explain how to validate corporate or business email address in asp.net using RegularExpression.

How to validate corporate or business email address in asp.net


Description:
Recently I have got a requirement, only business or corporate email address need to be entered. So I have to validate the free email address like Gmail, yahoo, Rediff, hotmail etc.

Implementation:

I have used the Regular expression validator control to validate. Add the below given code:
ValidationExpression="^[a-z0-9](\.?[a-z0-9])([\w-\.]+@(?!gmail.com)(?!yahoo.com)(?!hotmail.com)(?!rediffmail.com)(?!outlook.com)(?!zoho.com)([\w-]+\.)+[\w-]{2,4})?$"

Example:
Add a webform to project. Drag and drop the textbox and Regularexpressionvalidator from toolbox to webform

Complete HTML Markup of Webform:
  <fieldset style="width:50%">
    <legend>validate corporate email address </legend>
      <table>
    <tr><td>Enter Email :</td><td>
         <asp:TextBox ID="txtemail" runat="server"></asp:TextBox>
        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Please Enter Bussnies Email Id"         
            ValidationExpression="^[a-z0-9](\.?[a-z0-9])([\w-\.]+@(?!gmail.com)(?!yahoo.com)(?!hotmail.com)(?!rediffmail.com)(?!outlook.com)(?!zoho.com)([\w-]+\.)+[\w-]{2,4})?$"
            ControlToValidate="txtemail" ForeColor="#FF3300"></asp:RegularExpressionValidator></td></tr>
        <tr><td></td><td>
           </td></tr>
    </table>
    </fieldset>

Now run the project and test it.



No comments:

Post a Comment