News
  • Favorite
  • Facebook
  • Twitter
  • LinkedIn

Microsoft Press Pearson Education (InformIT)
Skip Navigation Links
Pearson Education (InformIT)
How to: Check that an email is in a valid format using Regular Expressions and C#

Sometimes you store users’ data in database and many users can change their data including their emails. Email addresses have a specific format like somename@domain.com. Some users may write any data in their emails. In this article we will make sure that the written email addresses follow this email address format using Regular Expressions and C#.

Using the Code:

To create an application that checks the format of email addresses, follow the following steps:

1.      Create a new windows application using Visual Studio 2005/2008/2010.

2.      Rename Form1 to EmailForm.

3.      Add a Label control and name it EmailLabel and set its Text property to Email.

4.      Add a TextBox Control and name it EmailTextBox.

5.      Add a Button and Name it CheckFormatButton and set it Text property to Check Email Format.

6.      Import the System.Text and System.Text.RegularExpressions namespaces using the following statement:

using System.Text;

using System.Text.RegularExpressions;

 

7.      The System.Text namespace contains classes that represent ASCII and Unicode character encodings; abstract base classes for converting blocks of characters to and from blocks of bytes; and a helper class that manipulates and formats String objects without creating intermediate instances of String.

8.      The System.Text.RegularExpressions namespace contains classes that provide access to the .NET Framework regular expression engine. The namespace provides regular expression functionality that may be used from any platform or language that runs within the Microsoft .NET Framework.

9.      Add the following method to the EmailForm.cs.

private bool IsValidEmail(string emailAddress)

{

    // Return true if emailAddress is in valid e-mail format.

    return Regex.IsMatch(emailAddress, @"^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$");

}

10.  Double click the CheckFormatButton to create the Button Click Event Handler.

11.  Add the following code to the CheckFormatButton Click Event Handler:

private void CheckFormatButton_Click(object sender, EventArgs e)

{

    if (this.IsValidEmail(EmailTextBox.Text))

    {

        MessageBox.Show(EmailTextBox.Text+" is in a valid email format.", "Valid Email",

            MessageBoxButtons.OK,MessageBoxIcon.Information);

    }

    else

    {

        MessageBox.Show(EmailTextBox.Text + " is not in a valid email format.","Invalid Email",

            MessageBoxButtons.OK,MessageBoxIcon.Exclamation);

    }

}

 

a.      In the above code we pass the text of the EmailTextBox to the IsValidEmail method.

b.     The IsValidEmail method uses the Regex class to find that the entered email address matches the Regular Expression.

c.      This Regular Expression checks that the entered text follows this pattern, starts with a text followed by @ character followed by a domain name followed by the (dot) character followed by the com or net or any other domain type. 

12.  Now compile and run the application.

13.  Enter an email address you want to check and click on the CheckFormatButton.

14.  You will be notified if the email address you typed is in a valid email address format or not.

Indicator_01

Indicator_01

Now you have an application that can check whether an email is in a valid email address format or not.

(416) Comments

Blog Message http://whiteivoryequestrian.weebly.com/blog/diverse-forms-of-surroundings-conditioning-units-for-distinct-chilling-and-heat-wants

I truly appreciate this post.Much thanks again. Great.


Blog Message http://trackgreenscore.com/trackMyGreenScore/blog/view/23103/the-best-way-to-download-files-from-the-net

Major thanks for the article post. Want more.


Blog Message http://fegolten.ch

Thankyou for this grand post, I am glad I observed this internet site on yahoo.


Blog Message http://www.driveteste.com.br/2013/01/21/hora-do-teste-pickup-ford-f75-1975/

Perfectly written content, thanks for selective information.


Blog Message http://inotechdc.com.br/manual/index.php?title=Why_you_should_set_up_a_successful_online_business2480083

My brother suggested I might like this web site. He was totally right. This post truly made my day. You cann at imagine just how much time I had spent for this information! Thanks!


Blog Message http://www.bubblesofbliss.com/2012/07/31/praesent-et-urna-turpis-sadips/

Really enjoyed this article.Thanks Again. Really Cool.


Blog Message http://www.floraservis.sk/forum/profile.php?id=49642

Wanted to drop a remark and let you know your Feed isnt working today. I tried adding it to my Yahoo reader account but got absolutely nothing.


Blog Message http://www.swimzania.com

I think this is a real great blog post.Thanks Again. Awesome.


Blog Message http://mallmesin.com/mesin-spiral-mixer-murah/

Enjoyed every bit of your blog.Much thanks again. Great.


Blog Message http://www.seoinvancouver.com/

Very good article.Much thanks again. Will read on...


Blog Message https://www.youtube.com/watch?v=Rzomzp4ALRg

Really enjoyed this blog.Much thanks again. Cool.


Blog Message http://www.placefinders.co.uk

My brother recommended I might like this blog. He was totally right. This post actually made my day. You cann at imagine just how much time I had spent for this information! Thanks!


Blog Message http://www.seoinvancouver.com/

I really liked your blog.Much thanks again. Want more.


Blog Message https://www.facebook.com/WishPromoCodeApril2018/

I value the post. Really Great.


Blog Message https://topbestbrand.com/%E0%B8%95%E0%B8%B4%E0%B8%94%E0%B8%95%E0%B8%B1%E0%B9%89%E0%B8%87%E0%B9%82%E0%B8%8B%E0%B8%A5%E0%B9%88%E0%B8%B2%E0%B9%80%E0%B8%8B%E0%B8%A5%E0%B8%A5%E0%B9%8C%E0%B9%80%E0%B8%AD%E0%B8%87/

I value the blog post.Really thank you! Great.

First Previous 1 2 3 4 5 6 7 8 9 10  ... Next Last 
Rating
Name
Email
Comment