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); } |
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); } } |
Note: you need to declare System.Text.RegularExpressions namespace in order for Regex class works properly.