Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home CSharp CSharp Validate Email Address Function With Regular Expression

CSharp Validate Email Address Function With Regular Expression

This is just a C# function that validate an email address to see whether it is a valid email format. The function uses Regular Expression to validate input email and an example in an ASP.net page will also be provided.

1. C# Validate Email Function

public bool validate_email_address(string email_address)
{
    Regex reg = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
 
    return reg.IsMatch(email_address);
}

public bool validate_email_address(string email_address) { Regex reg = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"); return reg.IsMatch(email_address); }

2. An Example In ASP.net page

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 csharp_aspnet_validate_email_address : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string email_address = "[email protected]";
 
        if (validate_email_address(email_address))
        {
            Response.Write(email_address + " is a VALID email address");
        }
        else
        {
            Response.Write(email_address + " is NOT a valid email address");
        }
 
        //[email protected] is a VALID email address 
    }
 
    public bool validate_email_address(string email_address)
    {
        Regex reg = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
 
        return reg.IsMatch(email_address);
    }
}

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 csharp_aspnet_validate_email_address : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string email_address = "[email protected]"; if (validate_email_address(email_address)) { Response.Write(email_address + " is a VALID email address"); } else { Response.Write(email_address + " is NOT a valid email address"); } //[email protected] is a VALID email address } public bool validate_email_address(string email_address) { Regex reg = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"); return reg.IsMatch(email_address); } }

Note: you need to declare System.Text.RegularExpressions namespace in order for Regex class works properly.

+ Download source code above

Feb 6, 2012Hoan Huynh
C# Check Google PageRank - ASP.NET ExampleTurn On - Turn Off Passcode In iPhone 4s
You Might Also Like:
  • CSharp Validate Internet URL Function With Regular Expression
  • Validate Email Address Format Using PHP Regular Expression preg_match
  • PHP validate ip address by using regular expression
  • Validate URL Use Regular Expression (Regex) In PHP
  • Auto Convert Text To URL Link And MailTo Email Address With ASP.NET C#
  • Validate email using regular expression in ASP.NET
  • C# ASP.Net Validate Date
  • String To Lower Case In PHP, JavaScript And .Net (CSharp)
  • String To Upper Case In PHP, JavaScript And .Net (CSharp)
  • Simple PHP Code Send Email
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

10 years ago CSharpemail address, Regex, regular expression, RegularExpressions102
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
24,451 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
21,829 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
17,646 views
JQuery Allow only numeric characters or only alphabet characters in textbox
14,984 views
C# Read Json From URL And Parse/Deserialize Json
11,710 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Things to Learn about Installingderm Loan Type S
  • Online Photo Editor – Free Photoediting Software
  • A Guide to Finding the Best Paper Sellers
  • Photoediting in Home Isn’t Hard to Do!

  • Free Photo Editor Online
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (27)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (647)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development