简体   繁体   English

电子邮件地址输入验证

[英]Email address input validation

Is there any way to make a textbox input validation for email addresses in wpf C#?有没有办法在 wpf C# 中对电子邮件地址进行文本框输入验证? Regex or validation expression or anything that can help, best with code sample and some instructions正则表达式或验证表达式或任何可以提供帮助的东西,最好使用代码示例和一些说明

On the text_changed event you could pass the value of the textbox to a helper class.在 text_changed 事件中,您可以将文本框的值传递给辅助类。

public static class ValidatorExtensions
{
    public static bool IsValidEmailAddress(this string s)
    {
        Regex regex = new Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");
        return regex.IsMatch(s);
    }
}

Now on the text changed event you can test the input现在在文本更改事件上,您可以测试输入

private void myTextBox_TextChanged(object sender, EventArgs e)
{
   bool result = ValidatorExtensions.IsValidEmailAddress( myTextBox.Text );
}

There are several ways to check if the email address is valid有几种方法可以检查电子邮件地址是否有效

About System.Net.Mail.MailAddress 关于System.Net.Mail.MailAddress
About Regex Expression 关于正则表达式

 static void Main(string[] args)
        {
            var validMail = "validMail@gmail.com";
            var invalidMail = "123";
            Console.WriteLine("IsValidMailAddress1 Test");
            Console.WriteLine(string.Format("Mail Address : {0} . is valid : {1}", validMail, IsValidMailAddress1(validMail)));
            Console.WriteLine(string.Format("Mail Address : {0} . is valid : {1}", invalidMail, IsValidMailAddress1(invalidMail)));

            Console.WriteLine("IsValidMailAddress2 Test");
            Console.WriteLine(string.Format("Mail Address : {0} . is valid : {1}", validMail, IsValidMailAddress2(validMail)));
            Console.WriteLine(string.Format("Mail Address : {0} . is valid : {1}", invalidMail, IsValidMailAddress2(invalidMail)));

        }


        static bool IsValidMailAddress1(string mail)
        {
            try
            {
                System.Net.Mail.MailAddress mailAddress = new System.Net.Mail.MailAddress(mail);

                return true;
            }
            catch
            {
                return false;
            }
        }

        static bool IsValidMailAddress2(string mailAddress)
        {
            return Regex.IsMatch(mailAddress, @"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
        }

这是我找到的最好的一个:

Regex.IsMatch(emailAddress, @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z", RegexOptions.IgnoreCase)

Actually, a great number of things have to be considered when validating email addresses:实际上,在验证电子邮件地址时必须考虑很多事情:

  • Nearly all Regex expressions found on the Internet are not 100% correct几乎所有在 Internet 上找到的 Regex 表达式都不是 100% 正确的

  • Even if the Regex is doing a 100% perfect job, the email client like Outlook or email server like Exchange might not accept the exotic, but valid email address.即使 Regex 做 100% 完美的工作,电子邮件客户端(如 Outlook)或电子邮件服务器(如 Exchange)可能不接受异国情调但有效的电子邮件地址。

Just one example of a strange, but valid email address:只是一个奇怪但有效的电子邮件地址的例子:

"very.(),:;<>[]\".VERY.\"very@\ \"very\".unusual"@strange.example.com

which is of the type:这是类型:

John.Doe@example.com

But actually, RFC 5322: Internet Message Format: Address Specification specifies that en email address looks like this:但实际上,RFC 5322:Internet 消息格式:地址规范指定 en 电子邮件地址如下所示:

John Doe<John.Doe@example.com>

The first "John Doe" is the display name.第一个“John Doe”是显示名称。

If you add display name validation, it becomes rather complicated.如果添加显示名称验证,它会变得相当复杂。 But even without display name, most regex fail to recognise the following email address as valid: "John@Doe"@example.com但即使没有显示名称,大多数正则表达式也无法将以下电子邮件地址识别为有效:“John@Doe”@example.com

Therefore the recommendation is just to warn the user if an email address looks strange, but to let the user decide if he made a typo or if he actually wants to enter that email address.因此,建议只是在电子邮件地址看起来很奇怪时警告用户,但让用户决定他是否打错了或者他是否真的想输入该电子邮件地址。

Additionally to validation, it might be helpful when the user can key in only legal characters.除了验证之外,当用户只能输入合法字符时,这可能会有所帮助。

Read my article CodeProject: Email Address Validation Explained , it comes with the code of a fully implemented and tested WPF Email Textbox.阅读我的文章CodeProject: Email Address Validation Explained ,它附带了一个完全实现和测试的 WPF 电子邮件文本框的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM