簡體   English   中英

如何確保用戶在文本框中輸入了電子郵件地址?

[英]How do I ensure that the user entered an email address in a textbox?

如何確保我的Web表單應用程序的用戶輸入了電子郵件地址? 我已經看過使用正則表達式和EmailAddress()的示例,但是如何在下面的if else語句中實現一個或另一個?

if (emailTextBox.Text == "" || emailTextBox.Text.Length > 100)
{
    emailErrorString = "Email: Enter email address.  No more than 100 characters.\n\n";
    emailString = null;
    errorMessage = true;
}
else
{
    emailString = emailTextBox.Text;
    emailErrorString = null;
}

我嘗試了下面的代碼,即使我輸入了一個無效的電子郵件地址“jj @jj。我沒有輸入”.com,或者net,或類似的東西,它也回來了:

 if (emailTextBox.Text == "" || emailTextBox.Text.Length > 100 ||                                          
      IsValid(emailTextBox.Text).Equals(false)) 
 {
     emailErrorString = "Email: Enter a valid email address. No more than 100
           characters.\n\n"; emailString = null; errorMessage = true; 
 } 
 else 
 { 
      emailString = emailTextBox.Text; emailErrorString = null; 
 }

您可以使用MailAddress類,如下所示:

public bool IsValid(string emailAddress)
{
    try
    {
        MailAddress m = new MailAddress(emailaddress);
        return true;
    }
    catch (FormatException)
    {
        return false;
    }
}

或者,您可以使用RegEx(您應該能夠找到適合驗證電子郵件地址的一個)。 此鏈接提供了可用字符/模式的基本概念: Regexlib

我嘗試使用MailAddress()示例,“jj @ jj”作為有效的電子郵件返回。 所以,我嘗試了以下內容,它完美地工作:

 ///Create a Regular Expression
 Regex regEmail = new Regex(@"^[\w!#$%&'*+\-/=?\^_`{|}~]+(\.[\w!#$%&'*+\-/=?
      \^_`{|}~]+)*"
        + "@" 
        + @"((([\-\w]+\.)+[a-zA-Z]{2,4})|(([0-9]{1,3}\.){3}[0-9]{1,3}))$");

和:

 ///test the email textbox against the created Regular Expression
 if (emailTextBox.Text == "" || emailTextBox.Text.Length > 100 || 
                !regEmail.IsMatch(emailTextBox.Text))
            {
                emailErrorString = "Email: Enter a valid email address.  No more than
                     100 characters.\n\n";
                emailString = null;
                errorMessage = true;
            }
            else
            {
                emailString = emailTextBox.Text;
                emailErrorString = null;
            }

好吧,如果它可以是任何類型的電子郵件地址,並且代碼不必檢查它是否有效,您可以使用此代碼,該代碼基於以下結構:

example@domain.extension

唯一的問題是檢查字符串是否包含@字符,a。 字符,以及有效的電子郵件域和擴展名(com / de / org / ...)。

public bool CheckAdress(string Adress)
{
    if (Adress.IndexOf('@') == -1)//if there are no @ characters in the Adress
    {
        return false;
    }
    switch (Adress.Substring(Adress.IndexOf('@') + 1, Adress.IndexOf('.') - Adress.IndexOf('@') + 1)//examines the domain between the @ and the. characters
    {
        case "gmail":
        case "freemail":
        case "citromail":
        //... (any valid domain name)
        break;
        default:
        return false;
    }
    switch (Adress.Substring(Adress.IndexOf('.') + 1, Adress.Length - Adress.IndexOf('.') + 1))//finally examines the extension
    {
        case "com":
        case "de":
        case "org":
        //... (any valid extension)
        break;
        default:
        return false;
    }

    //if all of these have not returned false, the adress might be valid, so
    return true;
}

此代碼僅在TextBox中沒有其他內容時才有效,只有相關的地址。 我知道這有點長,也許不是最完美的答案。 但是這樣您就可以自定義代碼接受哪些域和擴展,哪些不是。

但是,如果你想檢查這個電子郵件地址是否存在於現實中,我認為這個解決方案不起作用。

當長度超過100時,我沒有添加拋出異常的代碼,但您可以隨時添加它。

希望這個對你有幫助! :)

嘗試創建一個新的System.Net.Mail.MailAddress對象。 將用於用戶輸入的TextBox的Text屬性作為此構造函數的參數傳遞。 將其包裝在Try Catch塊中。 如果地址無效,您將獲得FormatException。

暫無
暫無

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

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