Page 1 of 1

NB function: evaluate string as alphanumeric

PostPosted: Thu Jul 19, 2012 1:46 pm
by dpayer
I struggled for nearly 2 hours trying to get several different 'canned' VB and JScript scripts to do a check for a string to see if it is alphanumeric (no non alphanumeric characters ex: !@#$%^&*() etc.).

Then I realized how easy it would be to do in NB. It took less than 10 min to put it together along with a demo app!

Here is the Neobook function you can save as a text file and save in your \functions folder under the NB install. There are two parameters: variable with string to be evaluated & result.


Code: Select all
{NeoBook Function}
Version=5.70
Param=[%1]|Variable|Variable with strring to be evaluated
Param=[%2]|Variable|NB Variable to show True/False (False return includes position of non alphanumeric character
{End}
SetVar "[AlphaNumList]" "abcdefghijklmnopqrstuvwxyz1234567890"
StrLen "[%1]" "[ThisStrLen]"
Loop "1" "[ThisStrLen]" "[WILL_Alpha]"
SubStr "[%1]" "[WILL_Alpha]" "1" "[testthis]"
.StickyNote "-1" "-1" "Testing [testthis]" "500"
SearchStr "[testthis]" "[AlphaNumList]" "[where_non_alphanum]" ""
   If "[where_non_alphanum]" "=" "0"
      SetVar "[%2]" "False,[WILL_Alpha]"
      ExitLoop
   Else
   SetVar "[%2]" "True"
   Endif
Endloop


Here is a demo pub that calls the function with the name 'isAlphaNumeric'

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
CompiledName=
CompileType=0
MediaType=0
FilesIncluded=1
FontsIncluded=2
FontsToCompile=|
CopyExternalFiles=No
SetupMedia=3
SetupInstDir=C:\MyProgram
SetupTitle=Setup
SetupColors=16711680|0|65535
Variables=[%1]¶[AlphaNumList]¶[result]¶[TextEntry1]¶[ThisStrLen]¶
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=32
Y=8
W=256
H=120
Anchor=0
VarName=[TextEntry1]
EditMultiLine=Yes
EditLen=0
LineColor=0
LineWidth=1
LineStyle=0
FillColor=16777215
FillPattern=0
Font=Arial
FontSize=10
FontStyle=0
FontCharset=1
TextColor=0
TabOrder=2
ObjectType=3
Name=PushButton1
X=88
Y=136
W=152
H=48
Anchor=0
Text=Alphnumeric?
Align=2
ImageStyle=0
XPTheme=Yes
ObjAction=Call "isAlphaNumeric" "[TextEntry1]" "[AlphanumTestResult]"¶AlertBox "Test Result" "[AlphanumTestResult]"¶SetVar "[TextEntry1]" ""
LineColor=0
LineWidth=1
LineStyle=0
FillColor=16777215
FillPattern=0
Font=Arial
FontSize=10
FontStyle=0
FontCharset=1
TextColor=0
TabOrder=3


Struggle?

PostPosted: Fri Jul 20, 2012 12:26 am
by reinier maliepaard
Hello David,

Thanks for your Neobook solution. However, your struggle wasn't necessary at all.

See: http://www.neosoftware.com/forum/viewtopic.php?t=19427

And download: www.neosoftware.com/software/mcStrings.zip

Study first button: 'Match Pattern' and look in the button properties.

You will see that I make a subtle difference between 'digit(s) OR letter(s)' and 'digit(s) AND letter(s)'...it's how you define alphanumeric!

SetVar "[[IP].Input]" "abcd1ef"
.SetVar "[[IP].Pattern]" "^[#91]0-9a-zA-Z[#93]+$"
.Test: digit(s) OR letter(s)?

SetVar "[[IP].Input]" "abcd1ef"
.SetVar "[[IP].Pattern]" "[#91]a-zA-Z[#93]+[#91]\d[#93]+"
.Test: digit(s) AND letter(s)?

Regards,
Reinier

PostPosted: Fri Jul 20, 2012 4:48 am
by Ndzn
Why not just use MATH on the string in question and check the result for "Error" ?????

Code: Select all
SetVar "[StrCheck]" "Bad Example"

Math "[StrCheck]+1" "-1" "[Result]"
If "[Result]" "=" "Error"
   AlertBox "ERROR" "THIS IS NOT A NUMBER!"
   Return
EndIf


"Keep it simple"

PostPosted: Fri Jul 20, 2012 7:04 am
by dpayer
Ndzn wrote:Why not just use MATH on the string in question and check the result for "Error" ?????

Code: Select all
SetVar "[StrCheck]" "Bad Example"

Math "[StrCheck]+1" "-1" "[Result]"
If "[Result]" "=" "Error"
   AlertBox "ERROR" "THIS IS NOT A NUMBER!"
   Return
EndIf


"Keep it simple"


The goal wasn't to determine if it is a number but to determine if each character was either a number or a letter. Goal was to catch strings that may have a ! @ # $ % ^ & * ( ) _ -= + or other non alphanumerics before it was processed.

This would have been a good job for Regular Expressions but it appears the VBS RegExp object throws an error instead of giving a return of false when evaluating a string. I couldn't get my error correction process to solve that.

David P.