Check whether given E-Mail is valid or not?

Posted by Vishalneeraj-24503 under Visual Studio category on | Points: 40 | Views : 384
Sometime,we may require to check whether Email is valid or not through code.This is useful when we send any mail from code,in that case we have to check.
So,by below code,we can check whether Email is valid or not using Regex :-
Import System.Text.RegularExpressions namespace at the top of the page.

Using System.Text.RegularExpressions;
public static bool IsValidEmail(string Email)
Regex reg_ex_Email = 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})(\]?)$");
return (reg_ex_Email.IsMatch(Email));

To check above function:-
bool is_valid1 = IsValidEmail(""); --True
bool is_valid2 = IsValidEmail("xyz"); --False

Comments or Responses

Login to post response