?
Solved

Check for illegal characters in an email address

Posted on 2008-11-03
12
Medium Priority
?
2,177 Views
Last Modified: 2013-11-25
I have a table of interal email addresses and need to check, when a user enters a new email address, that it does not contain any illegal characters.  The properform is firstnamelastname@thisbank.com.

However, I tried the attached which works but I would have to list out all the characters that are not allowed (except the @ sign).  I was just wondering if there is a better way?
SELECT tblEmailAddress.BR_NBR, tblEmailAddress.EmailAddressTo
FROM tblEmailAddress
WHERE (((tblEmailAddress.EmailAddressTo) Like '*!*' Or (tblEmailAddress.EmailAddressTo) Like '*?*'));

Open in new window

0
Comment
Question by:ssmith94015
  • 5
  • 4
  • 3
12 Comments
 
LVL 21

Expert Comment

by:silemone
ID: 22869784
[a-zA-Z][a-zA-Z0-9]*\@[a-zA-Z][a-zA-Z]*\.(com|us|net|org)
0
 
LVL 21

Expert Comment

by:silemone
ID: 22869797
(com|us|fr|net|org|ca|.......add all extension...above was example

0
 

Author Comment

by:ssmith94015
ID: 22869909
I think I understand.  Only I am looking for illegal characters, so I would reverse the seraching?  That is, rewriting my query to the new snippet?  What about quotes?
SELECT tblEmailAddress.BR_NBR, tblEmailAddress.EmailAddressTo
FROM tblEmailAddress
WHERE tblEmailAddress.EmailAddressTo NOT LIKE [a-zA-Z][a-zA-Z0-9]*\@[a-zA-Z][a-zA-Z]*\.com

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 21

Expert Comment

by:silemone
ID: 22869927
oh...sorry...you can't use regular expressions in sql...
not to tell you how to do this, but shouldn't you be checking before the user has a chance to enter data to table?
0
 

Author Comment

by:ssmith94015
ID: 22870011
Have that, but this is a fall-back just in case.
0
 
LVL 52

Accepted Solution

by:
Gustav Brock earned 500 total points
ID: 22870631
Here is a function for this.

/gustav
Function IsEmailAddress(ByVal strEmailAddr As String) As Boolean
 
' Checks if strEMailAddr could represent a valid e-mail address.
' Does not check validity of domain name.
 
  ' Allowed characters.
  Const cstrValidChars    As String = "@_-.0123456789abcdefghijklmnopqrstuvwxyz"
  Const cstrDot           As String = "."
  Const cstrAt            As String = "@"
  ' Minimum length of an e-mail address (a@a.ca).
  Const cintAddressLenMin As Integer = 6
  
  Dim strValidChars       As String
  Dim booFailed           As Boolean
  Dim intPos              As Integer
  Dim intI                As Integer
 
  ' Convert to lowercase.
  strEmailAddr = LCase(strEmailAddr)
  ' Check that strEMailAddr contains allowed characters only.
  For intI = 1 To Len(strEmailAddr)
    If InStr(cstrValidChars, Mid(strEmailAddr, intI, 1)) = 0 Then
      booFailed = True
    End If
  Next
  If booFailed = False Then
    ' Check that the first character is not cstrAt.
    booFailed = Left(strEmailAddr, 1) = cstrAt
    If booFailed = False Then
      ' Check that the first character is not a cstrDot.
      booFailed = Left(strEmailAddr, 1) = cstrDot
      If booFailed = False Then
        ' Check that length of strEMailAddr exceeds
        ' minimum length of an e-mail address.
        intPos = Len(strEmailAddr)
        booFailed = (intPos < cintAddressLenMin)
        If booFailed = False Then
          ' Check that none of the last two characters of strEMailAddr is a dot.
          booFailed = (InStr(intPos - 1, strEmailAddr, cstrDot) > 0)
          If booFailed = False Then
            ' Check that strEMailAddr does contain a cstrAt.
            intPos = InStr(strEmailAddr, cstrAt)
            booFailed = (intPos = 0)
            If booFailed = False Then
              ' Check that strEMailAddr does contain one cstrAt only.
              booFailed = (InStr(intPos + 1, strEmailAddr, cstrAt) > 0)
              If booFailed = False Then
                ' Check that the character leading cstrAt is not cstrDot.
                booFailed = (Mid(strEmailAddr, intPos - 1, 1) = cstrDot)
                If booFailed = False Then
                  ' Check that the character following cstrAt is not cstrDot.
                  booFailed = (Mid(strEmailAddr, intPos + 1, 1) = cstrDot)
                  If booFailed = False Then
                    ' Check that strEMailAddr contains at least one cstrDot
                    ' following the sign after cstrAt.
                    booFailed = Not (InStr(intPos, strEmailAddr, cstrDot) > 1)
                  End If
                End If
              End If
            End If
          End If
        End If
      End If
    End If
  End If
 
  IsEmailAddress = Not booFailed
 
End Function

Open in new window

0
 
LVL 21

Expert Comment

by:silemone
ID: 22870654
but he wants to do this in his sql cactus...
0
 
LVL 52

Expert Comment

by:Gustav Brock
ID: 22870929
Yes, but

> this is a fall-back just in case.

and this is the case. It is way too clumsy to do this in SQL. T-SQL or similar code perhaps, but not in a query.

/gustav
0
 

Author Comment

by:ssmith94015
ID: 22871194
Actually, the function is a good idea and thanks!

BTW, I'm a she
0
 

Author Comment

by:ssmith94015
ID: 22871211
silemone, thank you for trying to help, but I think catus_data's approach will fill my needs.  I was hoping to do this "my way", but it would be messy.  
0
 

Author Comment

by:ssmith94015
ID: 22871287
catus_data.  Thank you so much.  I copy/pasted into a module, called it in my query and works perfectly.  Thank you!  Part of this "fall back" was a recent developing of uploading the email addresses and I  need a way to quickly plow through them to be sure all are in the proper format, something I could not control via the input form.  I get a result set with the ones that need to be fixed-which was precsiely what I was looking form.

But silemone, you also gave me an idea on sometihng else so I do appreciate and thank you for your input.
0
 
LVL 52

Expert Comment

by:Gustav Brock
ID: 22871442
You are welcome!

/gustav
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Sometimes MS breaks things just for fun... In Access 2003, only the maximum allowable SQL string length could cause problems as you built a recordset. Now, when using string data in a WHERE clause, the 'identifier' maximum is 128 characters. So, …
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

850 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