Hi..!! i have created a Customer Details form in C# asking detail about customer...One of its field asks for customer email.. Please tell me how to validate a email id..Thnx..!!

Recommended Answers

All 6 Replies

You should look into Regex.

for this you have to use RegularExpression class in this there is Regex class. for email vallidation see this code.
using System.Text.RegularExpressions;
1)

public bool IsValidEmailAddress(string EmailAddress)
{
Regex regEmail = new Regex(@"^[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");
if(regEmail.IsMatch(EmailAddress))  ///in Email address write user email address
return true;
return false;
}

2)

string pattern=@"^[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|" +@"0-9]+([_][a-z|0-9]+)*)?@[a-z][a-z|0-9|]*\.([a-z]" + @"[a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$";
System.Text.RegularExpressions.Match match = Regex.Match(txtEmail.Text.Trim(), pattern, RegexOptions.IgnoreCase);

if(match.Success)  
  MessageBox.Show("Success");
else  
  MessageBox.Show("Fail");

try it works after this mark this thread as solved

commented: Read the rules +0

Thank you pritesh2010, now go and read the rules.

http://www.daniweb.com/forums/faq.php?faq=daniweb_policies

Do not post homework problems expecting a quick answer without showing any effort yourself. This especially pertains to the software development forums.

Please think about what you did here.

sorry for that next time i'll not give any one code without they show there effort need really.

don't apologize to me, it's OP's learning experience you took away, it's called no pain no gain.

If he took the effort in trying to figure it out he would learn it, copy paste ensures he just gets done.

for what purpose this regular expression is used ^[a-zA-Z0-9]+([_.-]?[a-zA-Z0-9]+)?@[a-zA-Z0-9]+([_-]?[a-zA-Z0-9]+)*([.]{1})[a-zA-Z0-9]+([.]?[a-zA-Z0-9]+)*$

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.