Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Code to validate an e-mail address in vb6

Posted on 2001-06-25
7
Medium Priority
?
231 Views
Last Modified: 2010-05-02
Code to validate an e-mail address in vb6
0
Comment
Question by:global819
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 5

Expert Comment

by:rkot2000
ID: 6225174
you can use instr to search for @, . , com or net
0
 
LVL 20

Expert Comment

by:hes
ID: 6225318
have a look at this example

http://cuinl.tripod.com/Tips/string1.htm
0
 
LVL 53

Accepted Solution

by:
Ryan Chong earned 200 total points
ID: 6225916
Hi, here is a sample:

Dim IsValid As Boolean
Dim InvalidReason As String
   
IsValid = IsEMailAddress("karl@karlmoore.com", InvalidReason)

MsgBox "Is the e-mail address valid? - " & IsValid

MsgBox "If invalid, the reason given is: " & InvalidReason

_______________________________________________________

Public Function IsEMailAddress(ByVal sEmail As String, _
    Optional ByRef sReason As String) As Boolean
       
     Dim sPreffix As String
     Dim sSuffix As String
     Dim sMiddle As String
     Dim nCharacter As Integer
     Dim sBuffer As String

     sEmail = Trim(sEmail)

     If Len(sEmail) < 8 Then
          IsEMailAddress = False
          sReason = "Too short"
          Exit Function
     End If


     If InStr(sEmail, "@") = 0 Then
          IsEMailAddress = False
          sReason = "Missing the @"
          Exit Function
     End If


     If InStr(InStr(sEmail, "@") + 1, sEmail, "@") <> 0 Then
          IsEMailAddress = False
          sReason = "Too many @"
          Exit Function
     End If


     If InStr(sEmail, ".") = 0 Then
          IsEMailAddress = False
          sReason = "Missing the period"
          Exit Function
     End If

     If InStr(sEmail, "@") = 1 Or InStr(sEmail, "@") = Len(sEmail) Or _
          InStr(sEmail, ".") = 1 Or InStr(sEmail, ".") = Len(sEmail) Then
          IsEMailAddress = False
          sReason = "Invalid format"
     Exit Function

End If


For nCharacter = 1 To Len(sEmail)
     sBuffer = Mid$(sEmail, nCharacter, 1)
     If Not (LCase(sBuffer) Like "[a-z]" Or sBuffer = "@" Or _
     sBuffer = "." Or sBuffer = "-" Or sBuffer = "_" Or _
     IsNumeric(sBuffer)) Then: IsEMailAddress = _
     False: sReason = "Invalid character": Exit Function
Next nCharacter

nCharacter = 0

On Error Resume Next

sBuffer = Right(sEmail, 4)
If InStr(sBuffer, ".") = 0 Then GoTo TooLong:
If Left(sBuffer, 1) = "." Then sBuffer = Right(sBuffer, 3)
If Left(Right(sBuffer, 3), 1) = "." Then sBuffer = Right(sBuffer, 2)
If Left(Right(sBuffer, 2), 1) = "." Then sBuffer = Right(sBuffer, 1)


If Len(sBuffer) < 2 Then
     IsEMailAddress = False
     sReason = "Suffix too short"
     Exit Function
End If

TooLong:

If Len(sBuffer) > 3 Then
     IsEMailAddress = False
     sReason = "Suffix too long"
     Exit Function
End If

sReason = Empty
IsEMailAddress = True

End Function
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:vim_asteya
ID: 6226455
u can try this too


Dim v1, v2
If Len(email.Text) = 0 Then
    DoEvents
ElseIf Len(email.Text) > 0 Then
    v2 = (InStr(1, email.Text, "@", vbTextCompare))
    v1 = (InStr(1, email.Text, ".", vbTextCompare))

        If ((v2 > 0) And (v1 > 0)) Then
            DoEvents
        Else
            MsgBox "Invalid ID", , "Email"
            email.SetFocus
            email.Text = ""
        End If
End If

I use this code in my projects
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7139799
Hi global819,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will suggest to:

    Accept ryancys's comment(s) as an answer.

global819, if you think your question was not answered at all or if you need help, you can simply post a new comment here.  Community Support moderators will follow up.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
0
 

Author Comment

by:global819
ID: 7140561
Thank you Randys
0
 
LVL 6

Expert Comment

by:Mindphaser
ID: 7144523
The answer is complete. I think that was a 'default' B, so I will change the grade to 'A'.

** Mindphaser - Community Support Moderator **
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question