我写了这个正则表达式函数
Public Function ValidateEmailAddress(ByVal txtEmailAddress As String) As Boolean
Dim pattern As String
pattern = "^[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*\\.([a-z]{2,4})$"
If Regex.IsMatch(txtEmailAddress, pattern) Then
Return True
Else
Return False
End If
End Function
在子程序中调用该函数:
If ValidateEmailAddress(txtEmailAddress.Text) = True Then
Else
MessageBox.Show("Email Not Valid")
End If
Sub Procedure
当我输入电子邮件时,正确或错误地我得到相同的消息,即电子邮件无效,除了在If语句之后单步执行代码,它忽略返回true。请问有人能发现问题所在吗?
答案 0 :(得分:2)
作为BigYellowCactus的答案的替代方案: - 您可以使用类似的东西 - 我认为这比使用正则表达式更可取:
Public Shared Function IsValidEmailAddress(ByVal emailAddress As String) As Boolean
Try
Dim address As New MailAddress(emailAddress)
Return True
Catch ex As Exception
Return False
End Try
End Function
这将返回False
@abc.com
和bill@
,而只检查@
将返回True