I have vb form tht accepts user data.I need to check whether user enters emailid in the correct format ?Please provide suggestion .Thanks in advance

Dani AI

Generated

As asked about validating an email while the user types and posted a Like-based VB check, here are a few practical, complementary points and alternatives you can apply depending on whether you’re on classic VB or VB.NET. Two rules to keep in mind: client-side checks are only syntactic (they don't prove the mailbox exists), and overly strict patterns will reject perfectly valid addresses (plus signs, apostrophes, long TLDs, IDNs, etc.).

For VB.NET a simple, reliable syntactic check is to let the framework parse the address with System.Net.Mail.MailAddress. It’s concise and handles many edge cases better than ad-hoc string comparisons:

' VB.NET - syntactic check using System.Net.Mail
Function IsEmailSyntacticallyValid(email As String) As Boolean
    If String.IsNullOrWhiteSpace(email) Then Return False
    email = email.Trim()
    Try
        Dim addr = New System.Net.Mail.MailAddress(email)
        Return True
    Catch ex As FormatException
        Return False
    End Try
End Function

If you need immediate inline feedback (fast client-side test), use a small pragmatic regex rather than an RFC-complete monster. This pattern is simple and practical for most forms:

' Simple, practical regex for inline validation
Imports System.Text.RegularExpressions
Function IsEmailSimpleRegex(email As String) As Boolean
    Dim pattern As String = "^[^@\s]+@[^@\s]+\.[^@\s]+$"
    Return Regex.IsMatch(email.Trim(), pattern, RegexOptions.IgnoreCase)
End Function

UX and troubleshooting tips: validate on the Validating event or on submit (not every keystroke) and show inline errors (ErrorProvider or a small helper text) instead of modal MsgBoxes. Always Trim input, normalize the domain to lower-case, and log examples that fail so you can adjust the pattern. For true verification, send a confirmation email—syntactic checks are only the first step. If you must support internationalized domains, convert them with IdnMapping.GetAscii before validation.

Recommended Answers

All 3 Replies

try this
first define a function

Private Function IsValidEmailAddress(ByVal sEmail As String, Optional ByRef sReason As String) As Boolean
   sEmail = LCase(Trim(sEmail))
   If Len(sEmail) < 7 Then
      sReason = "Too short"
   ElseIf sEmail Like "*[!0-9a-z@._+-]*" Then
      sReason = "Invalid character"
   ElseIf Not sEmail Like "*@*.*" Then
      sReason = "Missing the @ or ."
   ElseIf sEmail Like "*@*@*" Then
      sReason = "Too many @"
   ElseIf sEmail Like "[@.]*" Or sEmail Like "*[@.]" _
      Or sEmail Like "*..*" Or Not sEmail Like "?*@?*.*?" Then
      sReason = "Invalid format"
   Else
      Dim n As Integer
      n = Len(sEmail) - InStrRev(sEmail, ".")
      If n > 3 Then
         sReason = "Suffix too long"
      ElseIf n < 2 Then
         sReason = "Suffix too short"
      Else
         sReason = Empty
         IsValidEmailAddress = True
      End If
   End If
End Function

here call it (Normally On the lostfocus event)

Private Sub Text1_LostFocus()
Dim bool As Boolean, reason As String
bool = IsValidEmailAddress(Me.Text1.Text, reason)
If (bool <> True) Then
MsgBox reason
End If
End Sub

Hope this helps you . . .

its showing error for proper format of email

thank you for resolving :)kindly ignore the former message

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.