簡體   English   中英

如何檢查 email 在 c# 中的格式是否有效

[英]how to check if email is in valid format or not in c#

我的代碼是任何人都可以糾正這個問題或解決這個問題的任何其他方法。 這是我到目前為止所得到的,但它似乎沒有用:

  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') 

嘗試這個:

using System.ComponentModel.DataAnnotations;
public bool IsValidEmail(string email)
 {
    return new EmailAddressAttribute().IsValid(email);
 }

...或這個

bool IsValidEmail(string email)
{
try {
    var addr = new System.Net.Mail.MailAddress(email);
    return addr.Address == email;
}
catch {
    return false;
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM