Email Validation using C#

Today, I will teach you how to create a program that determines whether an inputted email is valid or not using C#. I have already a tutorial in this with vb.net but now I used C# here. So, now let's start this tutorial! 1. Let's start with creating a Windows Form Application in C# for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application and name your program ValidateEmail. 2. Next, add a TextBox named TextBox1 for the inputting of an e-mail address and one Button named Button1 that will determine if the email is valid or not. You must design your interface like this: design 3. Import a namespace named System.Text.RegularExpressions because we will use regular expression function to commnunicate with our input.
  1. using System.Diagnostics;
  2. using System;
  3. using System.Windows.Forms;
  4. using System.Collections;
  5. using System.Drawing;
  6. using System.Data;
  7. using System.Collections.Generic;
  8.  
  9. using System.Text.RegularExpressions;
4. Now, put this code in Button1_Click. This will display the result of the valid or invalid email address you've entered in the TextBox.
  1. public void Button1_Click(System.Object sender, System.EventArgs e)
  2. {
  3. string expression = default(string);
  4. expression = "^([0-9a-zA-Z]([-\\.\\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})$";
  5. if (Regex.IsMatch(TextBox1.Text, expression))
  6. {
  7. MessageBox.Show("Valid Email address ");
  8. }
  9. else
  10. {
  11. MessageBox.Show("Not a valid Email address ");
  12. }
  13. }
We initialized expression As string to hold the string value of "^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$". C# also provides an extensive set of regular expression tools that enable to efficiently compare strings as well as rapidly parse large amounts of text and data to search for, remove, and replace text patterns for validating an email address. Regex.IsMatch(TextBox1.Text, expression) - this syntax indicates whether the specified regular expression finds a match in the specified input string of our textbox and compare its regular expression to our variable expression. If the comparing was right, then it will display, "Valid Email address". Otherwise, it will prompt the user "Not a valid Email address ". Press F5 to run the program. Output: outputoutputFull Source code:
  1. using System.Diagnostics;
  2. using System;
  3. using System.Windows.Forms;
  4. using System.Collections;
  5. using System.Drawing;
  6. using System.Data;
  7. using System.Collections.Generic;
  8.  
  9. using System.Text.RegularExpressions;
  10.  
  11. namespace ValidateEmail
  12. {
  13. public partial class Form1
  14. {
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. }
  19.  
  20.  
  21. public void Button1_Click(System.Object sender, System.EventArgs e)
  22. {
  23. string expression = default(string);
  24. expression = "^([0-9a-zA-Z]([-\\.\\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})$";
  25. if (Regex.IsMatch(TextBox1.Text, expression))
  26. {
  27. MessageBox.Show("Valid Email address ");
  28. }
  29. else
  30. {
  31. MessageBox.Show("Not a valid Email address ");
  32. }
  33. }
  34. }
  35.  
  36. }
Download the source code below and try it! :) Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment