Source language: Translate to:

VBS Function: validate email (uses regex)

Questions and information about using VBScript and JavaScript in NeoBook functions

Moderator: Neosoft Support

VBS Function: validate email (uses regex)

Postby dpayer » Thu Jul 05, 2012 10:46 am

I have adapted a simple VB script from ASPFAQs.com to use RegEx to validate an email address. Note: this does not check if the address is a working email. It simply verifies the formatting (someone @ somedomain).

Original regex only checked for TLDs with 2-3 characters, ie .us, .eu, .com, .org, etc.. (this means .info or other similar domains won't validate if you don't tweek the script.)

I have edited the regex (thanks to www.regular-expressions.info ) and it now works properly with all valid domains.

David P.

Here is the function:
Code: Select all
{NeoBook Function}
Version=5.70
Language=VBScript
Param=[%1]|Variable|Variable containing email to verify
Param=[%2]|Variable|Variable to return True/False if is a valid email
{End}
emailAddress = publication.nbGetVar( "[%1]" )
  if emailAddress <> "" then
    blnValidEmail = RegExpTest(emailAddress)
    if blnValidEmail then
     
      publication.nbSetVar "[%2]", "True"
    else
      publication.nbSetVar "[%2]", "False"
    end if
  end if

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

  ' Create regular expression:
  regEx.Pattern ="\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b"

  ' 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


Here is a sample pub (I have named the function: RegEx_ValidateEmail )

Code: Select all
NBW Publication Source File
(C)1993-2011 NeoSoft Corp.
Title=Untitled NeoBook Publication
Language=US
NeoBookVer=5.70
ScreenColors=16777216
ScreenWidth=320
ScreenHeight=200
BkndColor=0
ScreenAutoSize=Yes
LinkColor=16711680
WindowStyle=0
WindowOrder=0
WindowPos=0
WindowProps=31
BiDiMode=0
UseUpdateTimer=Yes
ReturnTime=60
ShapeTransColor=536870912
CompileType=0
MediaType=0
FilesIncluded=1
FontsIncluded=2
CopyExternalFiles=No
SetupMedia=3
SetupInstDir=C:\MyProgram
SetupTitle=Setup
SetupColors=16711680|0|65535
Variables=[textentry1]¶
VersionMajor=1
VersionMinor=0
VersionRelease=0
VersionBuild=0
ReqExLevel=1
CustomFontName=Arial
CustomFontSize=9
CustomFontStyle=0
CustomCharset=1
CacheSize=30
ExtractOp=132
L_OK=OK
L_CANCEL=Cancel
L_YES=Yes
L_NO=No
L_CLOSE=Close
L_ERRORTITLE=Error
L_FINDTITLE=Find
L_FINDPROMPT=Find &What
L_FINDFIRST=&Find First
L_FINDNEXT=Find &Next
L_FINDERROR=Search text not found.
L_PWORDTITLE=Password required
L_PWORDMSG=Before continuing, you must first|enter the correct password.
L_PWORDPROMPT=&Password:
L_PWORDERROR=That's not the right password!
L_MEDIAERROR=In order to play this type of file you must install|the appropriate multimedia hardware and/or|software.
L_INTERNETERROR=Unable to locate Internet Browser software.
L_ABORTFUNCTION=Do you want to cancel this function?
L_EXTRACTERROR=This file could not be extracted and cannot be played.
L_INSTDIRPROMPT=Please enter the drive and directory where you would like this program to be installed. To accept the default drive and directory, simply press Enter.
L_INSTFOLDERPROMPT=Do you wish to create a program folder?
L_INSTRUNPROMPT=Do you want to run
L_INSTDISKPROMPT=Please insert disk #
L_INSTDISKERROR=That's not the correct disk, try again.
L_INSTGENERROR=The installation has terminated.
L_INSTEXITPROMPT=Do you really want to cancel the installation?
L_INSTWINTITLE=Setup
L_INSTPROGTITLE=Extracting Files...
L_SENDEMAIL=Sending Mail...
L_MAILSERVER=Please enter the name of your outgoing email server:
L_COPY=Copy to Clipboard
L_SELECTALL=Select All
L_DONWLOADFILE=Downloading: %s
L_PLEASEWAIT=Please wait...
TrayMenuBegin
MenuName=TrayOpen
MenuText=Open
MenuState=3
MenuKey=0
MenuAction=SetVar "[WindowState]" "Normal"
MenuName=TrayExit
MenuText=Exit
MenuState=3
MenuKey=0
MenuAction=Exit "" ""
MenuEnd
MasterPage
NewPage=New Page
PageStyle=0
PageColor=16777215
PageGradient1=16711680
PageGradient2=255
PageGradientDir=1
WallpaperStyle=0
ObjectType=9
Name=TextEntry1
X=24
Y=38
W=280
H=32
Anchor=0
VarName=[TextEntry1]
EditLen=0
LineColor=0
LineWidth=1
LineStyle=0
FillColor=16777215
FillPattern=1
Font=Arial Rounded MT Bold
FontSize=10
FontStyle=0
FontCharset=1
TextColor=0
TabOrder=2
ObjectType=3
Name=PushButton1
X=184
Y=86
W=120
H=40
Anchor=0
Text=Test
Align=2
ImageStyle=0
XPTheme=Yes
DepressEffect=No
ObjAction=If "[textentry1]" "=" ""¶Alertbox "Empty" "nothing entered"¶Else¶Call "RegEx_ValidateEmail" "[textentry1]" "[isavalidemail]"¶Alertbox "Testing email address" "Is [textentry1] a valid email?||[isavalidemail]"¶Endif
LineColor=0
LineWidth=1
LineStyle=0
FillColor=16777215
FillPattern=1
FillMode=Yes
Font=Arial Rounded MT Bold
FontSize=10
FontStyle=0
FontCharset=1
TextColor=0
TabOrder=3
ObjectType=8
Name=Text1
X=24
Y=8
W=184
H=32
Anchor=0
Font=Arial Rounded MT Bold
FontSize=10
FontStyle=0
FontCharset=1
TextColor=0
Text={\rtf1\ansi\deff0\deftab254{\fonttbl{\f0\fnil\fcharset1 Arial Rounded MT Bold;}}{\pard{\ql\li0\fi0\ri0\sb0\sl\sa0 \plain\f0\fs20\cf0 Enter email address}}}
HMargin=4
VMargin=4
LineColor=0
LineWidth=0
LineStyle=0
FillColor=16777215
FillPattern=1
TabOrder=4
Last edited by dpayer on Wed Jul 11, 2012 8:47 am, edited 1 time in total.
User avatar
dpayer
 
Posts: 1380
Joined: Mon Apr 11, 2005 5:55 am
Location: Iowa - USA

Postby Neosoft Support » Fri Jul 06, 2012 10:40 am

That's very useful. Thank you for sharing it with us.
NeoSoft Support
Neosoft Support
NeoSoft Team
 
Posts: 5593
Joined: Thu Mar 31, 2005 10:48 pm
Location: Oregon, USA


Return to NeoBook Functions - VBScript & JavaScript

Who is online

Users browsing this forum: No registered users and 1 guest

cron