Connecting Tech Pros Worldwide Forums | Help | Site Map

testing emails

WindAndWaves
Guest
 
Posts: n/a
#1: Nov 13 '05
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





WindAndWaves
Guest
 
Posts: n/a
#2: Nov 13 '05

re: testing emails


"WindAndWaves" <access@ngaru.com> wrote in message
news:3x1yd.251$mo2.21944@news.xtra.co.nz...[color=blue]
> Hi Folks\[/color]

Note two corrections, sorry.
[color=blue]
> 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[/color]
gives[color=blue]
> 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[/color]
should be atp + 1
[color=blue]
> '--- check dot
> D1p = InStr(AtP, S, ".", vbTextCompare)[/color]

should be atp + 1
[color=blue]
> 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[/color]
122,[color=blue]
> 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
>
>[/color]


Bas Cost Budde
Guest
 
Posts: n/a
#3: Nov 13 '05

re: testing emails


WindAndWaves wrote:
[color=blue][color=green]
>>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.[/color][/color]

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.
[color=blue][color=green]
>>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.[/color][/color]

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
WindAndWaves
Guest
 
Posts: n/a
#4: Nov 13 '05

re: testing emails



"Bas Cost Budde" <b.costbudde@heuvelqop.nl> wrote in message
news:cqbaf4$4cg$1@news2.solcon.nl...[color=blue]
> WindAndWaves wrote:
>[color=green][color=darkred]
> >>I have written a new function that tests emails. Use it if you like,[/color][/color][/color]
but[color=blue][color=green][color=darkred]
> >>what I was wondering is if this is good coding. Anyone with any
> >>suggestions, please let me know.[/color][/color]
>
> 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.
>[color=green][color=darkred]
> >>The other thing I wanted to know is if the function can return both a[/color][/color][/color]
true[color=blue][color=green][color=darkred]
> >>or a false value and a message (string) with the error description.[/color][/color]
>
> 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[/color]
small"[color=blue]
> 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[/color]


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


Bas Cost Budde
Guest
 
Posts: n/a
#5: Nov 13 '05

re: testing emails


WindAndWaves wrote:
[color=blue]
> 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.[/color]

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
WindAndWaves
Guest
 
Posts: n/a
#6: Nov 13 '05

re: testing emails



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

[.....]
[color=blue]
> Where is NZ?[/color]

www.newzealand.com



Closed Thread