-2

My code is this can anyone correct this or any other solution to this problem. This is what I've got so far but it doesn't seem to be working:

  int i=0,f=0;
    string n = Console.ReadLine();
    for (i = 0; i < n.Length; i++)          
        if(n[i]=='@' || n[i] == '.')              
            f = f + 1;               
    if(f==2){
console.writeline('correct')
}
    else{
console.writeline('Incorrect') 
  • 1
    Possible duplicate of [C# code to validate email address](http://stackoverflow.com/questions/1365407/c-sharp-code-to-validate-email-address) – Maksim Simkin Jan 21 '17 at 11:49

2 Answers2

2

Try this:

using System.ComponentModel.DataAnnotations;
public bool IsValidEmail(string email)
 {
    return new EmailAddressAttribute().IsValid(email);
 }
kritikaTalwar
  • 1,730
  • 1
  • 17
  • 25
0

...or this

bool IsValidEmail(string email)
{
try {
    var addr = new System.Net.Mail.MailAddress(email);
    return addr.Address == email;
}
catch {
    return false;
}
}
PeterK
  • 4,243
  • 4
  • 44
  • 74