473,466 Members | 1,301 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

testing emails

Hi Folks

I have written a new function that tests emails. Use it if you like, but
what I was wondering is if this is good coding. Anyone with any
suggestions, please let me know.

The other thing I wanted to know is if the function can return both a true
or a false value and a message (string) with the error description.

TIA

- Nicolaas

Public Function TestEmail(S As String) As Boolean
'checks if an email address is valid
Dim i As Long
Dim j As Long
Dim AtP As Long ' position of at sign
Dim D1p As Long ' position of first dot
Dim L As Long ' L = length of string
Dim CC As Byte ' character code, made byte specifically so that it gives
an error if it is over or under its range
'--- check length
L = Len(S)
If L < 7 Then GoTo xt 'smallest email x@x.com = 7 characters
'---check @ sign
AtP = InStr(1, S, "@", vbTextCompare)
If AtP < 2 Or AtP > (L - 3) Then GoTo xt
If InStr(AtP, S, "@", vbTextCompare) > 0 Then GoTo xt
'--- check dot
D1p = InStr(AtP, S, ".", vbTextCompare)
If D1p < 1 Then GoTo xt
'--- check characters
For i = 1 To L
CC = Asc(Mid(S, i, 1))
Select Case CC
Case 36, 37, 38, 43, 45, 46, 48 To 57, 64 To 90, 95, 97 To 122,
192 To 246, 249 To 255
'DO NOTHING
Case Else
GoTo xt
End Select
Next i
TestEmail = True
xt:
Exit Function
er:
Resume xt
End Function
Nov 13 '05 #1
5 1441
"WindAndWaves" <ac****@ngaru.com> wrote in message
news:3x*****************@news.xtra.co.nz...
Hi Folks\
Note two corrections, sorry.
I have written a new function that tests emails. Use it if you like, but
what I was wondering is if this is good coding. Anyone with any
suggestions, please let me know.

The other thing I wanted to know is if the function can return both a true
or a false value and a message (string) with the error description.

TIA

- Nicolaas

Public Function TestEmail(S As String) As Boolean
'checks if an email address is valid
Dim i As Long
Dim j As Long
Dim AtP As Long ' position of at sign
Dim D1p As Long ' position of first dot
Dim L As Long ' L = length of string
Dim CC As Byte ' character code, made byte specifically so that it gives an error if it is over or under its range
'--- check length
L = Len(S)
If L < 7 Then GoTo xt 'smallest email x@x.com = 7 characters
'---check @ sign
AtP = InStr(1, S, "@", vbTextCompare)
If AtP < 2 Or AtP > (L - 3) Then GoTo xt
If InStr(AtP, S, "@", vbTextCompare) > 0 Then GoTo xt should be atp + 1
'--- check dot
D1p = InStr(AtP, S, ".", vbTextCompare)
should be atp + 1
If D1p < 1 Then GoTo xt
'--- check characters
For i = 1 To L
CC = Asc(Mid(S, i, 1))
Select Case CC
Case 36, 37, 38, 43, 45, 46, 48 To 57, 64 To 90, 95, 97 To 122, 192 To 246, 249 To 255
'DO NOTHING
Case Else
GoTo xt
End Select
Next i
TestEmail = True
xt:
Exit Function
er:
Resume xt
End Function

Nov 13 '05 #2
WindAndWaves wrote:
I have written a new function that tests emails. Use it if you like, but
what I was wondering is if this is good coding. Anyone with any
suggestions, please let me know.
I don't like GoTo. Not in a high-level language, at least. It makes the
code logic hard to follow.

I learned to structure my logic and program flow in diagrams
(Nassi-Schneider type) where there is no representation for goto.

I will put my interpretation below.
The other thing I wanted to know is if the function can return both a true
or a false value and a message (string) with the error description.


A function cannot return (=be evaluated as) two values. But you can have
parameters that the function changes. In fact, if you don't specify
otherwise, every parameter to a function can be changed inside the
function. I usually denote this with a prefix VAR in the variable name.

Public Function TestEmail(S As String, varResult As String) As Boolean
'checks if an email address is valid
Dim i As Long
Dim j As Long
Dim AtP As Long ' position of at sign
Dim D1p As Long ' position of first dot
Dim L As Long ' L = length of string
Dim CC As Byte ' character code, made byte specifically so that it gives
'an error if it is over or under its range
Dim bRes As Boolean
bRes = True 'assume valid until proven otherwise
varResult = ""
'--- check length
L = Len(S)
If L < 7 Then
bRes = False 'smallest email x@x.com = 7 characters
varResult = varResult & "; too short"
End If
'---check @ sign
AtP = InStr(1, S, "@", vbTextCompare)
If AtP < 2 Or AtP > (L - 3) Then
bRes = False
varResult = varResult & "; portion before or after @ sign too small"
End If
If InStr(AtP, S, "@", vbTextCompare) > 0 Then
bRes = False
varResult = varResult & "; more than one @ sign"
End If
'--- check dot
D1p = InStr(AtP, S, ".", vbTextCompare)
If D1p < 1 Then
bRes = False
varResult = varResult & "; missing dot"
End If
'--- check characters
For i = 1 To L
Select Case Asc(Mid(S, i, 1))
Case 36, 37, 38, 43, 45, 46, 48 To 57, 64 To 90, 95, 97 To 122,
192 To 246, 249 To 255
'DO NOTHING
Case Else
bRes = False
varResult = varResult & "; invalid character at position " & i
End Select
Next i
xt:
varResult = Mid(varResult, 3) 'cut off the first delimiter
TestEmail = bRes
Exit Function
er:
varResult = varResult & "; " & Err.Description
Resume xt
End Function

--
Bas Cost Budde, Holland
http://www.heuveltop.nl/BasCB/msac_index.html
I prefer human mail above automated so in my address
replace the queue with a tea
Nov 13 '05 #3

"Bas Cost Budde" <b.*********@heuvelqop.nl> wrote in message
news:cq**********@news2.solcon.nl...
WindAndWaves wrote:
I have written a new function that tests emails. Use it if you like, butwhat I was wondering is if this is good coding. Anyone with any
suggestions, please let me know.
I don't like GoTo. Not in a high-level language, at least. It makes the
code logic hard to follow.

I learned to structure my logic and program flow in diagrams
(Nassi-Schneider type) where there is no representation for goto.

I will put my interpretation below.
The other thing I wanted to know is if the function can return both a trueor a false value and a message (string) with the error description.

A function cannot return (=be evaluated as) two values. But you can have
parameters that the function changes. In fact, if you don't specify
otherwise, every parameter to a function can be changed inside the
function. I usually denote this with a prefix VAR in the variable name.

Public Function TestEmail(S As String, varResult As String) As Boolean
'checks if an email address is valid
Dim i As Long
Dim j As Long
Dim AtP As Long ' position of at sign
Dim D1p As Long ' position of first dot
Dim L As Long ' L = length of string
Dim CC As Byte ' character code, made byte specifically so that it gives
'an error if it is over or under its range
Dim bRes As Boolean
bRes = True 'assume valid until proven otherwise
varResult = ""
'--- check length
L = Len(S)
If L < 7 Then
bRes = False 'smallest email x@x.com = 7 characters
varResult = varResult & "; too short"
End If
'---check @ sign
AtP = InStr(1, S, "@", vbTextCompare)
If AtP < 2 Or AtP > (L - 3) Then
bRes = False
varResult = varResult & "; portion before or after @ sign too

small" End If
If InStr(AtP, S, "@", vbTextCompare) > 0 Then
bRes = False
varResult = varResult & "; more than one @ sign"
End If
'--- check dot
D1p = InStr(AtP, S, ".", vbTextCompare)
If D1p < 1 Then
bRes = False
varResult = varResult & "; missing dot"
End If
'--- check characters
For i = 1 To L
Select Case Asc(Mid(S, i, 1))
Case 36, 37, 38, 43, 45, 46, 48 To 57, 64 To 90, 95, 97 To 122,
192 To 246, 249 To 255
'DO NOTHING
Case Else
bRes = False
varResult = varResult & "; invalid character at position " & i
End Select
Next i
xt:
varResult = Mid(varResult, 3) 'cut off the first delimiter
TestEmail = bRes
Exit Function
er:
varResult = varResult & "; " & Err.Description
Resume xt
End Function

Thank your for your input Bas. The reason I used the goto was to make the
function as fast as possible. If, for example, it has less than 7
characters then there is no point in checking other stuff. The function is
meant to test several thousand email addresses so you want it to be fast.

Having said that, I saw some other nice ideas that I would like to use.
Thank you.

Groet uit NZ
Thiemen
Nov 13 '05 #4
WindAndWaves wrote:
Thank your for your input Bas. The reason I used the goto was to make the
function as fast as possible. If, for example, it has less than 7
characters then there is no point in checking other stuff. The function is
meant to test several thousand email addresses so you want it to be fast.

Having said that, I saw some other nice ideas that I would like to use.
Thank you.


Do nested Ifs if you want to gain speed. GoTo must be interpreted, the
False condition in the If simply jumps to the End If (my bet is that
they both get executed as jumps=gotos at the machine level)

Where is NZ?
--
Bas Cost Budde, Holland
http://www.heuveltop.nl/BasCB/msac_index.html
I prefer human mail above automated so in my address
replace the queue with a tea
Nov 13 '05 #5

"Bas Cost Budde" <b.*********@heuvelqop.nl> wrote in message

[.....]
Where is NZ?


www.newzealand.com

Nov 13 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: simonc | last post by:
I've been programming in assembler and C/C++ for a number of years, but I'm only just starting down the road of PHP & MYSQL. I have a couple of questions: (1) Before I start writing my own...
1
by: dan glenn | last post by:
I'm creating HTML emails from a PHP site and sending them out to an email list (just about 40 people so far are on this list). I've tested and confirmed that these emails work in yahoo.com's...
0
by: Thomas Mandelid | last post by:
A client I`m working for want to be able to send multiple emails to customers of their own. I have developed a simple app to accomplish this, but the application has a few flaws and I`m looking for...
4
by: ItsMe | last post by:
Dear All, I'm getting junk email with around 140-150 kb almost everyday and it my mailbox is getting full and this emails are full of virus effected attachments. And I'm getting these emails...
5
by: Kun | last post by:
i have the following code: ---------------------------------- import smtplib from email.MIMEText import MIMEText fp = open('confirmation.txt', 'rb') msg = MIMEText(fp.read()) From =...
5
by: sck10 | last post by:
Hello, I am working on a new project where I need to: 1. open an email and then create an xml file from the email 2. open the xml file and parse the information. I would like to be able to do...
5
by: Jai | last post by:
Hi, I am in a problem of sending mass emails(newsletter) to my website members. Actually my problem is this: I want to send newsletter to my website members. But I had given a facility for...
2
by: =?Utf-8?B?RGFuY2Vy?= | last post by:
Hi, I was attempting to check my new incoming emails through Outlook Express (I have Windows 98, 2nd Edition). My computer seemed to be having a problem accessing and opening the emails, and the...
0
by: =?Utf-8?B?Q2hhcmxlcw==?= | last post by:
Like many people, I normally use Yahoo! Mail via the web and like to keep all my emails stored on the Yahoo! server. However sometimes I can’t get access to a PC/the web and I download my emails...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.