Email validation : Check email format By using regular expression


A regular expression known as regex in short  is a special text string which describes  a search pattern.

Here is a c# function which accept  a string as input parameter and return true if string is valid Email address  and  return  false if string is not valid .

For this function to work properly you have to add these namespaces.


using System.Text;
using System.Text.RegularExpressions;



   protected bool checkEmail(string Emailtext)

    {

     Regex MyEmailRegex = new Regex(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");

     if (string.IsNullOrEmpty(Emailtext))

     {

         return false;

     }

     else

     {

         return MyEmailRegex.IsMatch(Emailtext);

     }

   }

By using this function you can check that Email format is a valid email format or not on server side.in my next article I will explain how you can check for email format using javascript.
Tags: , , ,

Join Us!