【问题标题】:In C# windows forms how a MaskedTextBox for email address can be implemented在 C# windows 窗体中,如何实现用于电子邮件地址的 MaskedTextBox
【发布时间】:2012-11-11 05:14:39
【问题描述】:
   public void Form1_Load(Object sender, EventArgs e) 
    {
          // Other initialization code
         mtxtEmailID.Mask = ".........."; 

掩码类型应该是什么来代替点

         mtxtEmailID.MaskInputRejected += new MaskInputRejectedEventHandler(mtxtEmailID_MaskInputRejected)
    }

   void mtxtEmailID_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
   {
      if(!Regex.IsMatch(txtEmailID.Text, "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"))  

这里的正则表达式给了我错误,让我知道什么是正确的电子邮件验证。

         {
         toolTip1.ToolTipTitle = "Invalid Input";
         toolTip1.Show("Enter valid email address", mtxtEMailID);
         }
   }

【问题讨论】:

  • MaskedTextBox 不够通用,无法验证电子邮件地址。
  • @Bridge:我刚刚浏览了 RFC,感谢您的建议。每个答案都启发了我
  • @SteveWellens:这不是最简单的。还是有更简单的验证。帮助将不胜感激。因为将正则表达式代码放入我的代码空间看起来有点奇怪。
  • 我会按照下面答案中的建议使用 MailAddress 类。

标签: c# regex winforms email-validation maskedtextbox


【解决方案1】:

您可以找到有关 MaskedTextBox here的信息


如果您想验证电子邮件地址,Regex 不是正确的选择。regex 无法涵盖许多极端情况...

使用MailAddress

try 
{
   address = new MailAddress(address).Address;
   //email address is valid since the above line has not thrown an exception
} 
catch(FormatException) 
{
   //address is invalid
}

但如果你需要正则表达式,它应该是:

.+@.+

【讨论】:

  • 让我尝试这两种方法.. 但完整的正则表达式让我感到害怕。它工作得很好吗?它肯定会占用大量的代码空间。
  • @Sri 不要使用那么大的regex。使用上面给出的MailAddress 类..不要使用那个大的regex
  • 我想知道如何使用工具以及在哪里使用 try 和 catch 方法。因为电子邮件 ID 输入将从文本框中获取。
【解决方案2】:

我的项目中的这种方法使电子邮件验证变得简单,只考虑了电子邮件中重要的几个因素,例如“@”和“。” .我觉得不要让它变得复杂,因为每个人的电子邮件地址不是强制性的。

    private void txtEmailID_Validating(object sender, System.ComponentModel.CancelEventArgs e)
    {
        string errorMsg;
        if (!ValidEmailAddress(txtEmailID.Text, out errorMsg))
        {
            // Cancel the event and select the text to be corrected by the user.
            e.Cancel = true;
            txtEmailID.Select(0, txtEmailID.Text.Length);

            // Set the ErrorProvider error with the text to display.  
            this.errorProvider1.SetError(txtEmailID, errorMsg);
        }
    }


    public bool ValidEmailAddress(string txtEmailID, out string errorMsg)
    {
        // Confirm that the e-mail address string is not empty. 
        if (txtEmailID.Length == 0)
        {
            errorMsg = "e-mail address is required.";
            return false;
        }

        // Confirm that there is an "@" and a "." in the e-mail address, and in the correct order.
        if (txtEmailID.IndexOf("@") > -1)
        {
            if (txtEmailID.IndexOf(".", txtEmailID.IndexOf("@")) > txtEmailID.IndexOf("@"))
            {
                errorMsg = "";
                return true;
            }
        }

        errorMsg = "e-mail address must be valid e-mail address format.\n" +
           "For example 'someone@example.com' ";
        return false;
    }

    private void txtEmailID_Validated(object sender, EventArgs e)
    {
        // If all conditions have been met, clear the ErrorProvider of errors.
        errorProvider1.SetError(txtEmailID, "");
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-11
    • 1970-01-01
    • 2014-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-17
    • 2015-01-23
    相关资源
    最近更新 更多