2
   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);
         }
   }
4

2 回答 2

3

您可以在此处找到有关 MaskedTextBox 的信息


如果您想验证电子邮件地址正则表达式不是正确的选择。有许多极端情况regex不会涵盖...

使用邮件地址

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

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

.+@.+
于 2012-11-22T19:29:33.307 回答
0

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

    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, "");
    }
于 2012-11-23T11:28:33.487 回答