test string for email address (validate an email address)

Example scripts and tips (replaces Old Scripts & Tips archive)

Moderators: Dorian (MJT support), JRL, Phil Pendlebury

Post Reply
kpassaur
Automation Wizard
Posts: 696
Joined: Wed Jul 07, 2004 1:55 pm

test string for email address (validate an email address)

Post by kpassaur » Sun Feb 15, 2009 10:00 am

I though this maybe useful to some. I saw it posted on line in Java and converted it to MS. It tests a variable to see if it is an email address.

It does not confirm the address only the format.

It makes sure there is an "@" symbol, one character before it and that it ends in a 2, 3, or four character extension after the last period.



//Test to see if an email address
//Test for @
//Test for Character before @
//Test to see that after the @ symbol it ends in a 2,3 or four extenssion after the period.


Let>deliveraddress=[email protected]

GoSub>validateemailadd
IF>test=YES
MDL>%deliveraddress% is an email address
Endif

IF>test=NO
MDL>%deliveraddress% is not an email address
Endif

SRT>validateemailadd
Position>@,%deliveraddress%,1,t
If>t=0,notanemail
Separate>%deliveraddress%,@,addarray
If>ADDARRAY_COUNT=2
Length>%ADDARRAY_1%,t
If>t=0,notanemail
Separate>ADDARRAY_2,.,suffex
Let>s=suffex_COUNT
If>%s%=0,notanemail
Let>ext=suffex_%s%
Length>%ext%,t
IF>{(%t% > 1) AND (%t% haveanemail
Endif
Else
Goto>notanemail
Endif
Label>notanemail
Let>test=NO
Goto>donetest
Label>haveanemail
Let>test=YES
Label>donetest
END>validateemailadd

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Sun Feb 15, 2009 4:37 pm

Regular Expressions are usually used for this sort of thing. Here's a VBScript function for validating an email address:

Code: Select all

VBSTART
Function RegExpTest(sEmail)
  RegExpTest = false
  Dim regEx, retVal
  Set regEx = New RegExp

  ' Create regular expression:
  regEx.Pattern ="^[\w-\.]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,4}$"

  ' Set pattern:
  regEx.IgnoreCase = true

  ' Set case sensitivity.
  retVal = regEx.Test(sEmail)

  ' Execute the search test.
  If not retVal Then
    exit function
  End If

  RegExpTest = true
End Function
VBEND
Test it with:

Code: Select all

VBEval>RegExpTest("[email protected]"),isValid
MessageModal>isValid

VBEval>RegExpTest("bad_444%boo.do"),isValid
MessageModal>isValid
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts