什么是电子邮件地址的最佳正则表达式验证 [英] Whats the best regex validation for email address

查看:32
本文介绍了什么是电子邮件地址的最佳正则表达式验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用正则表达式为我的注册表单添加电子邮件验证

I want to include email validation for my register form using regular expression

我已尝试使用此代码正则表达式来验证我的电子邮件

I've tried this code regex for validating my email

Dim ValidateEmail As Boolean
ValidateEmail = Regex.IsMatch(EmailAddress.Text, "^([\w]+)@([\w]+)\.([\w]+)$", RegexOptions.IgnoreCase)

我试图在我的电子邮件地址中输入一些电子邮件.文本是我的变量名,但在输入一些点后出现错误:jannus.domingo@yahoo.com在 Janus 之后,点是我得到的错误,但在我删除 jannus.domingo@yahoo.com 上的点之后,就可以了.

I tried to input some email in my EmailAddress. The text which is my variable name, but I got an error after putting some dot ex: jannus.domingo@yahoo.com after Janus the dot is the error I got but after I removed dot on jannus.domingo@yahoo.com then it's fine.

推荐答案

与其纠结于不可读、冗长、难以维护且难以维护的正则表达式,只需尝试创建 MailAddress 类以及您从用户那里获得的字符串.如果出现以下情况,它将抛出 FormatException格式无效.

Instead of messing about with unreadable, long, hard and unmaintainable regular expression, simply try to create an instance of the MailAddress class with the string you get from the user. It will throw a FormatException if the format is invalid.

这是一个简单的例子:

Function IsEmailAddressWellFormatted(ByVal address As String) As Boolean
    Try
        Dim address = New MailAddress(address)
        Return True
    Catch ex As FormatException
        Return False 
        ' We don't care about the actual exception here, 
        ' the fact that it's thrown is enough to know the string is not a valid format.
    End Try
End Function

这篇关于什么是电子邮件地址的最佳正则表达式验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆