Email Checker Program in C#

Regular Expression

  • A Regular Expression is a set of characters that specify a pattern. It comes from a term used to describe grammars and formal language.
  • All browsers that validate the email of the user use a Regular Expression.
  • Regular Expressions are used when you want to search for specific lines of text containing a specific pattern.
  • Regular Expressions search for patterns on a single line, and not for patterns that start on one line and end on another.
  • Regular Expressions are more powerful and flexible. You can search for words of a certain size and Numbers, punctuation characters, you name it, a Regular Expression can find it.

Note

For checking we will use using System.Text.RegularExpressions;

Rules

  1. ^ from this our Regular Expression will start.
  2. $ At this sign our Regular Expression will end.
  3. + will concatenate the string.
  4. [a-zA-Z] This portion will restrict the user to enter always the first character as a letter because all standard emails must start with a letter.
  5. [a-zA-Z0-9] In this portion we can input alphabets and numbers.
  6. [[a-zA-Z0-9-_.!#$%'*+/=?^] In this portion we can input numbers, letters and special characters as described. All three of these portions will be concatenated with the + sign.
  7. {1,20} It tells the length of the email address.
  8. @[a-zA-Z0-9]{1,20} Here in this after @Regular Expression is a built in function that checks the domain pattern that we define. And also tells the pattern and length.
  9. .[a-zA-Z]{2,3}$ In this after it will check for .com or .pk or .in or .us.

Regular Expression

Diagram

Regex regex1 = new Regex("^[a-zA-Z]+[a-zA-Z0-9]+[[a-zA-Z0-9-_.!#$%'*+/=?^]{1,20}@[a-zA-Z0-9]{1,20}.[a-zA-Z]{2,3}$");

Email checker

Program

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using System.Text.RegularExpressions;

 

namespace CFGMailChecker

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

           //Regular Expression

            Regex regex1 = new Regex("^[a-zA-Z]+[a-zA-Z0-9]+[[a-zA-Z0-9-_.!#$%'*+/=?^]{1,20}@[a-zA-Z0-9]{1,20}.[a-zA-Z]{2,3}$");

 

          //This function will check whether the expression is right or not.

         

                if (!regex1.IsMatch(textBox1.Text))

                {

                    MessageBox.Show("Your email address format is not correct!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);

                }

                else

                {

                    MessageBox.Show("Congratulations Your email address format is correct!", "CORRECT", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);

                }                                 

          }

    }

}

I have also attached the source code so you can download it and run in your PC.


Recommended Free Ebook
Similar Articles