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.

(500) Comments

Blog Message http://bathbusinessweb.co.uk/common-pitfalls-avoid-website-designed/

This awesome blog is definitely cool and informative. I have found a bunch of helpful tips out of it. I ad love to visit it again soon. Thanks!


Blog Message http://adep.kg/user/quetriecurath998/

I value the blog article.Thanks Again. Really Great.


Blog Message http://btcearn.ga/story.php?title=tadalista-com

Thanks so much for the post. Really Cool.


Blog Message http://wyw.wapbox.ru/out.php?url=http://haydenreynell.blogspot.com

Your style is really unique in comparison to other people I ave read stuff from. Thanks for posting when you have the opportunity, Guess I will just bookmark this site.


Blog Message http://apptube.podnova.com/go/?go=http://faheemhutchinson.blogspot.com

Since the admin of this web page is working,


Blog Message http://jelly-life.com/2018/07/26/weekend-starting-hours-for-shops-in-america-2/

Im thankful for the blog article.Much thanks again. Awesome.


Blog Message http://network-resselers.com/2018/07/26/grocery-stores-starting-hours-in-the-us/

I went over this site and I believe you have a lot of great info , saved to bookmarks (:.


Blog Message http://bestfluremedies.com/2018/07/26/christmas-and-thanksgiving-opening-hours-in-the-usa/

Run on hills to increase your speed. The trailer for the movie


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

Wow, awesome blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your site is fantastic, as well as the content!


Blog Message http://donorbrick.net/__media__/js/netsoltrademark.php?d=yanisfields.blogspot.com

Way cool! Some very valid points! I appreciate you penning this write-up and the rest of the site is extremely good.


Blog Message https://www.fresh-taste-catering.com/

Thank you ever so for you article.Much thanks again. Keep writing.


Blog Message http://media-partner.info/%D0%B5%D0%BA%D1%81%D0%BF%D0%B5%D1%80%D1%82-%D0%BE%D1%82-%D1%84%D0%B1%D1%80-%D0%B4%D0%B0%D0%B2%D0%B0-%D1%81%D1%8A%D0%B2%D0%B5%D1%82%D0%B8-%D0%BA%D0%B0%D0%BA-%D0%B4%D0%B0-%D0%BD%D0%B0%D0%BA%D0%B0/

Wow, incredible blog format! How long have you ever been blogging for? you made running a blog glance easy. The overall glance of your site is great, let alone the content!


Blog Message http://images.google.fi/url?q=http://maisonhulme.blogspot.com

Thanks for any other fantastic post. Where else may just anybody get that type of info in such a perfect method of writing? I have a presentation next week, and I am at the look for such information.


Blog Message http://womens-loungewear-pants.my-free.website/

Usually I don at read post on blogs, but I would like to say that this write-up very forced me to try and do so! Your writing style has been amazed me. Thanks, quite nice post.


Blog Message https://vimeo.com/user87135825

This particular blog is without a doubt interesting and besides factual. I have picked up helluva interesting things out of this source. I ad love to return again soon. Thanks a lot!

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