Skip to main content

ASP.Net - Regular Expression to validate email id in C#.net



ASPX Code:

<!DOCTYPE html>
<html>
<head id="Head1" runat="server">
    <title>ASP.Net - Regular Expression To Validate Email Address in C#.Net</title>
</head>
<body>
    <form id="frmValidate" name="frmValidate" runat="server">
    <div>
        <table border="1" cellpadding="5">
            <tr>
                <td>
                    Enter Email Id Here
                </td>
                <td>
                    <asp:TextBox ID="txtEmailId" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                    <asp:Button ID="btnValidateEmail" runat="server" Text="Check" OnClick="btnValidateEmail_Click" />
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <p style="color: Red;">
                        <asp:Label ID="lblValidEmail" runat="server"></asp:Label></p>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

 C#.Net Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

public partial class ValidateEmail : System.Web.UI.Page
{
    protected void btnValidateEmail_Click(object sender, EventArgs e)
    {
        //Check Email Id is valid
        if (IsValidEmailId(txtEmailId.Text))
        {
            lblValidEmail.Text = txtEmailId.Text + " is valid email";
        }
        else
        {
            lblValidEmail.Text = txtEmailId.Text + " is not valid email";
        }
    }
    //This function returns true if the emailid matches the pattern. Else it will return false
    private bool IsValidEmailId(string InputEmail)
    {
        //Regex To validate Email Address
        Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
        Match match = regex.Match(InputEmail);
        if (match.Success)
            return true;
        else
            return false;
    }

}

 Equivalent VB.Net Code;

Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Text.RegularExpressions

Partial Public Class ValidateEmail
    Inherits System.Web.UI.Page
    Protected Sub btnValidateEmail_Click(sender As Object, e As EventArgs)
        'Check Email Id is valid
        If IsValidEmailId(txtEmailId.Text) Then
            lblValidEmail.Text = txtEmailId.Text + " is valid email"
        Else
            lblValidEmail.Text = txtEmailId.Text + " is not valid email"
        End If
    End Sub
    'This function returns true if the emailid matches the pattern. Else it will return false
    Private Function IsValidEmailId(InputEmail As String) As Boolean
        'Regex To validate Email Address
        Dim regex As New Regex("^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$")
        Dim match As Match = regex.Match(InputEmail)
        If match.Success Then
            Return True
        Else
            Return False
        End If
    End Function

End Class



Comments

  1. I need regular expression validation like below
    +(country code)-(area code)-(fax number)
    for example: +1-212-9876543
    with error-messages like if miss '-' then show 'missing '-''.
    if miss '+' then show 'missing '+''.

    ReplyDelete
  2. After numerous StackOverflow research, and a few hours trying to code one myself....nothing.

    **head bangs against wall**

    One more attempt at researching a Regex.. and it works flawlessly! You're a genius!

    ReplyDelete

Post a Comment

Popular posts from this blog

Code to create log files in C#.Net|Asp.Net

Introduction: In my previous article I have explained about how to create, delete and check whether the directory exists using C#.Net . In this article I am going to explain about  How to create log files in C#.Net. Explanation: Log files are useful to track any runtime errors and exceptions in all the applications. Below code will code will get the Message and Pagename as the input and creates the log file in that date. For that first i have imported below two namespaces.

Dynamically programmatically add contols at run time Asp.Net

Introduction: In my previous article I have explained about What is View State? How to Store and retrieve values from View State . In this article I am going to explain how to add controls programmatically on run time. Explanation: This example adds a text box and button to a Web Forms page at run time. It also dynamically binds an event handler to the button's   Click   event. The handler displays the values of the dynamically generated text box. The controls are added into a   Panel   Web server control, which acts as a placeholder. The controls are separated in the panel with line breaks (HTML <BR> elements), which are added to the panel using the   LiteralControl   control.

Code To Convert rupees(numbers) into words using C#.Net

Introduction: In my previous article I have explained about how to validate emailid using javascript . In this article I am going to explain about code used to convert rupees(numbers) into words using C#.Net . Explanation: For explanation purpose I have a page. It has a textbox to input the numbers. And when you click on the convert to words button then it will convert the input numbers into words and shows it in the below label. Below is the C# code used to do this functionality. public static string NumbersToWords( int inputNumber) {     int inputNo = inputNumber;     if (inputNo == 0)         return "Zero" ;     int [] numbers = new int [4];     int first = 0;     int u, h, t;     System.Text. StringBuilder sb = new System.Text. StringBuilder ();     if (inputNo < 0)     { ...