Subscribe to the ASP.NET Robot RSS Feed

Email Validation




I searched the internet and I found that one of the common questions is how I can validate users email address from code behind.
So In this tutorial I will show you how you can easily validate email address from code behind Using Regular Expression .
For example add one textbox to your page with id=”txtEmail” ,  add one label with id=”lblResult” and add one button, Add this code to the button onclick event:

 

string pattern = @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";

        Match match = Regex.Match(txtEmail.Text.Trim(), pattern, RegexOptions.IgnoreCase);

        if (match.Success)
           lblResult.Text = "Valid";
        else
            lblResult.Text = "Not Valid";

 

Don’t forget to add a namespace to the top of the code behind page:

using System.Text.RegularExpressions;

Add New Comment:

Facebook del.icio.us FriendFeed Digg it Google Buzz Linked in MySpace Reddit Stumbleupon Twitter Technorati