'how to check if email is in valid format or not in c#
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')
Solution 1:[1]
Try this:
using System.ComponentModel.DataAnnotations;
public bool IsValidEmail(string email)
{
return new EmailAddressAttribute().IsValid(email);
}
Solution 2:[2]
...or this
bool IsValidEmail(string email)
{
try {
var addr = new System.Net.Mail.MailAddress(email);
return addr.Address == email;
}
catch {
return false;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | PeterK |
