Validating e-mail with regular expression VB.Net

后端 未结 6 1753
没有蜡笔的小新
没有蜡笔的小新 2020-12-05 16:13

I\'m working on a small project in VB.Net where I get a input from a textbox, and need to verify that this is an e-email address.

I found this expression \"^[_a-z0-9

6条回答
  •  春和景丽
    2020-12-05 16:29

    Use the System.Text.RegularExpressions.Regex class:

    Function IsEmail(Byval email as string) as boolean
        Static emailExpression As New Regex("^[_a-z0-9-]+(.[a-z0-9-]+)@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,4})$")
    
        return emailExpression.IsMatch(email)
    End Function
    

    The most important thing to understand about this answer is that I didn't write the regular expression myself. There are just so many wrong ways that seem to be right, and there are several levels of detail that you could take this to. For example, do you want to restrict this to valid top level domains, and if so, how are you accounting for the fact that they are now occasionally adding new TLDs? If the regular expression the most appropriate place for that test, or should have separate code for that check? Even the expression in this answer is now very stale since it was originally authored.

    I recommend finding an outside resource for the expression you know will be maintained over time.

提交回复
热议问题