tags:

views:

170

answers:

2

How to use Ajax in ASP.NET for Email ID validation.

+1  A: 

You can use regular expression to validate an email format . Here is an article about that. How to Find or Validate an Email Address . You can add a regular expression validator and set the required regular expression.

If you are looking for validating an email (exist or does not exist) then there is no other way other than sending the email and checking if it bounces back.

Here is not article for .net and about regular expression validator

Please explain more if I am wrong.

Shoban
A: 

Since you do not give a specific case. so let me share my registration page code snippet. Here is how I use ajax to check if an email address has been registered.

on register.aspx page:

<asp:UpdatePanel ID="UpdatePanel_CheckEmail" 
  UpdateMode="Conditional" runat="server">

  <ContentTemplate>
    <asp:Label ID="LblEmail" AssociatedControlID="TxtEmail" 
      runat="server">
      <span>*</span>Email:
    </asp:Label>

    <!-- Server side validation -->
    <asp:TextBox ID="TxtEmail" 
       ontextchanged="TxtEmail_TextChanged" 
       AutoPostBack="true" runat="server" />

    <small>
      <asp:Literal ID="LblEmailStatus" runat="server" Text="" />
    </small>

    <!-- Client side validation -->
    <asp:RegularExpressionValidator ID="TxtEmailRegEx" runat="server" 
       ErrorMessage="Enter a valid email address to sign up"
       ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" 
       ControlToValidate="TxtEmail" />

  </ContentTemplate>
</asp:UpdatePanel>

on register.aspx.cs:

protected void TxtEmail_TextChanged(object sender, EventArgs e)
{
    // Server side validation
    if ( EmailRegistered(TxtEmail.txt) )
    {
       LblEmailStatus.Text = "use other email!";
    }
}
Anwar Chandra