Source language: Translate to:

Validate IP address subroutine

NeoBook tips, tricks, code samples and more...

Moderator: Neosoft Support

Validate IP address subroutine

Postby dpayer » Tue Oct 28, 2008 2:23 pm

I needed to use a text entry tool to enter IP addresses to be queried.

To be sure that there were no data errors, I created a subroutine that works in conjunction with the text entry tool.

Hints:
1) Use a validation mask of 999.999.999.999
This will allow the entry of only numbers between the dots.
2) Set the default value to be 000.000.000.000

The routine strips out any possible spaces that come from numbers going in the wrong way and it resets if you leave out an octet.

There are places in the code where I had test alertboxes just to catch my errors and I left them there.

Feel free to use as you please.

David Payer
NOTE: the code below can be copied and saved as a *.pub file which can be opened in NeoBook (I used 5.6 but this should work with older versions) and you can play with it there.

Code: Select all
NBW Publication Source File
(C)1993-2008 NeoSoft Corp.
Title=IP Validator
Author=David Payer
Language=US
NeoBookVer=5.60
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
SubroutineAction=:validIP¶StrReplace "[ip]" "   " "" "[ip]" ""¶StrReplace "[ip]" "  " "" "[ip]" ""¶StrReplace "[ip]" " " "" "[ip]" ""¶¶¶StrParse "[ip]" "." "[octet]" "[howmanyoctet]"¶         .mask should elliminate any errors¶         If "[howmanyoctet]" "<>" "4"¶            AlertBox "Error" "4 octets required!"¶            SetVar "[ip]" "000.000.000.000"¶            Return¶         Endif¶Loop "1" "4" "[loopcount]"¶     StrLen "[octet[loopcount]]" "[octetlength]"¶     .AlertBox "test" "Octet is [octetlength]. The value is [octet[loopcount]]"¶             IF "[octetlength]" "=" "1"¶               SetVar "[octet[loopcount]]" "00[octet[loopcount]]"¶               Else¶                   IF "[octetlength]" "=" "2"¶                   SetVar "[octet[loopcount]]" "0[octet[loopcount]]"¶                   Endif¶             Endif¶     IfEx "[octet[loopcount]]>255 OR [octet[loopcount]]<0"¶            AlertBox "Error" "Data Entry Error - out of range. Octet must be 0-255."¶            SetVar "[ip]" "000.000.000.000"¶            Return¶            Endif¶            ¶            .AlertBox "test" "beggining pad routine"¶            ¶     SubStr "[octet[loopcount]]" "1" "1" "[firstone]"¶     SubStr "[octet[loopcount]]" "1" "2" "[firsttwo]"¶     ¶            If "[firsttwo]" "=" "00"¶               SubStr "[octet[loopcount]]" "3" "1" "[octet[loopcount]]"¶               Else¶               If "[firstone]" "=" "0"¶               SubStr "[octet[loopcount]]" "2" "2" "[octet[loopcount]]"¶               Endif¶            Endif¶            ¶            .Alertbox "test" "ending pad routine"¶            ¶      If "[octet[loopcount]]" "=" ""¶            AlertBox "Error" "Data Entry Error - out of range. Octet must be 0-255."¶            SetVar "[ip]" "000.000.000.000"¶            Return¶            Endif¶            ¶¶.alertbox "test" "ending loop routine"¶            ¶EndLoop¶¶SetVar "[ip]" "[octet1].[octet2].[octet3].[octet4]"¶AlertBox "Success!" "Validated IP is [IP]"¶            ¶Return
ShapeTransColor=536870912
CompileType=0
MediaType=0
FilesIncluded=1
FontsIncluded=2
CopyExternalFiles=No
SetupMedia=3
SetupInstDir=C:\MyProgram
SetupTitle=Setup
SetupColors=16711680|0|65535
Variables=[howmanyoctet]¶[ip]¶[octet]¶
VersionMajor=1
VersionMinor=0
VersionRelease=0
VersionBuild=0
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=IPNumber
X=31
Y=73
W=121
H=26
Anchor=0
Text=000.000.000.000
VarName=[IP]
EditMask=999.999.999.999
EditLen=15
LineColor=0
LineWidth=1
LineStyle=0
FillColor=16777215
FillPattern=0
Font=Arial
FontSize=10
FontStyle=0
FontCharset=1
TextColor=0
TabOrder=0
ObjectType=8
Name=Label
X=37
Y=50
W=122
H=18
Anchor=0
Font=Arial
FontSize=10
FontStyle=0
FontCharset=1
TextColor=0
Text={\rtf1\ansi\deff0\deftab254{\fonttbl{\f0\fnil\fcharset1 Arial;}}{\colortbl\red0\green0\blue0;\red255\green0\blue0;\red0\green128\blue0;\red0\green0\blue255;\red255\green255\blue0;\red255\green0\blue255;\red128\green0\blue128;\red128\green0\blue0;\red0\green255\blue0;\red0\green255\blue255;\red0\green128\blue128;\red0\green0\blue128;\red255\green255\blue255;\red192\green192\blue192;\red128\green128\blue128;\red0\green0\blue0;\red0\green0\blue0;}\paperw12240\paperh15840\margl1880\margr1880\margt1440\margb1440\margh720\margf720{\*\pnseclvl1\pnucrm\pnstart1\pnhang\pnindent720{\pntxtb}{\pntxta{.}}}¶{\*\pnseclvl2\pnucltr\pnstart1\pnhang\pnindent720{\pntxtb}{\pntxta{.}}}¶{\*\pnseclvl3\pndec\pnstart1\pnhang\pnindent720{\pntxtb}{\pntxta{.}}}¶{\*\pnseclvl4\pnlcltr\pnstart1\pnhang\pnindent720{\pntxtb}{\pntxta{)}}}¶{\*\pnseclvl5\pndec\pnstart1\pnhang\pnindent720{\pntxtb{(}}{\pntxta{)}}}¶{\*\pnseclvl6\pnlcltr\pnstart1\pnhang\pnindent720{\pntxtb{(}}{\pntxta{)}}}¶{\*\pnseclvl7\pnlcrm\pnstart1\pnhang\pnindent720{\pntxtb{(}}{\pntxta{)}}}¶{\*\pnseclvl8\pnlcltr\pnstart1\pnhang\pnindent720{\pntxtb{(}}{\pntxta{)}}}¶{\*\pnseclvl9\pndec\pnstart1\pnhang\pnindent720{\pntxtb{(}}{\pntxta{)}}}¶\endnhere\sectdefaultcl{\pard{\ql\li0\fi0\ri0\sb0\sl\sa0 \plain\f0\fs16\cf0 Enter  IP Address}}¶}
HMargin=1
VMargin=1
LineColor=0
LineWidth=0
LineStyle=0
FillColor=16777215
FillPattern=0
TabOrder=3
ObjectType=3
Name=PushButton1
X=170
Y=70
W=93
H=31
Anchor=0
Text=Validate
Align=2
ImageStyle=0
XPTheme=Yes
ObjAction=GoSub "validIP"
LineColor=0
LineWidth=0
LineStyle=0
FillColor=12632256
FillPattern=0
Font=Arial
FontSize=10
FontStyle=0
FontCharset=1
TextColor=0
TabOrder=4
User avatar
dpayer
 
Posts: 1380
Joined: Mon Apr 11, 2005 5:55 am
Location: Iowa - USA

MAYBE...

Postby virger » Wed Oct 29, 2008 8:44 am

TRY THIS...
FROM COSTA RICA
PURA VIDA
---------------------
{NeoBook 5 Objects}
NeoBookVer=5.60
ObjectType=9
Name=TextEntry6
X=26
Y=116
W=159
H=32
Anchor=0
Text=000.000.000.000
VarName=[ip]
EditMask=000.000.000.000
EditLen=15
ObjAction=StrReplace "[ip]" "[#32]" "0" "[ip]" ""¶strparse "[ip]" "." "[oc]" "[n]"¶loop "1" "[n]" "[c]"¶ if "[oc[c]]" ">" "255"¶ alertbox "" "ERROR"¶ exitloop¶ endif¶endloop
LineColor=0
LineWidth=1
LineStyle=0
FillColor=15724527
FillPattern=0
Font=Arial
FontSize=14
FontStyle=0
FontCharset=1
TextColor=0
TabOrder=13
User avatar
virger
 
Posts: 509
Joined: Mon Sep 18, 2006 12:21 pm
Location: Costa Rica, America Central

Re: MAYBE...

Postby dpayer » Wed Oct 29, 2008 1:14 pm

virger wrote:TRY THIS...
FROM COSTA RICA
PURA VIDA
---------------------
{NeoBook 5 Objects}
NeoBookVer=5.60
ObjectType=9


Thanks. That is much smaller to be sure and it works.

There is an issue if you don't want preceeding zeros in an octet or if you simply want to type a single digit then a dot to move to the next octet set which is how most IP entry forms work.

So you would have to inform the user that if he wants

4.16.252.2 as an address he will have to type 004.016.252.002

David P.
User avatar
dpayer
 
Posts: 1380
Joined: Mon Apr 11, 2005 5:55 am
Location: Iowa - USA

OKI...

Postby virger » Thu Oct 30, 2008 7:48 am

{NeoBook 5 Objects}
NeoBookVer=5.60
ObjectType=9
Name=TextEntry7
X=36
Y=146
W=159
H=32
Anchor=0
Text=000.000.000.000
VarName=[ip]
EditMask=000.000.000.000
EditLen=15
ObjAction=StrReplace "[ip]" "[#32]" "0" "[ip]" ""¶strparse "[ip]" "." "[oc]" "[n]"¶loop "1" "[n]" "[c]"¶ if "[oc[c]]" ">" "255"¶ alertbox "" "ERROR"¶ gotoline "BAD"¶ endif¶ math "[oc[c]]" "" "[xoc[c]]"¶ if "[xoc[c]]" "<" "10"¶ setvar "[xoc[c]]" "00[xoc[c]]"¶ else¶ if "[xoc[c]]" "<" "100"¶ setvar "[xoc[c]]" "0[xoc[c]]"¶ endif¶ endif¶endloop¶setvar "[ip]" "[xoc1].[xoc2].[xoc3].[xoc4]"¶¶:BAD
LineColor=0
LineWidth=1
LineStyle=0
FillColor=15724527
FillPattern=0
Font=Arial
FontSize=14
FontStyle=0
FontCharset=1
TextColor=0
TabOrder=11


PURA VIDA
FROM COSTA RICA
User avatar
virger
 
Posts: 509
Joined: Mon Sep 18, 2006 12:21 pm
Location: Costa Rica, America Central

Re: OKI...

Postby dpayer » Thu Oct 30, 2008 11:08 am

virger wrote:{NeoBook 5 Objects}
NeoBookVer=5.60
ObjectType=9
. . . . . . . . . . .
PURA VIDA
FROM COSTA RICA


Your coding is "tighter" than mine. :)

Thanks for the examples!

David P.
User avatar
dpayer
 
Posts: 1380
Joined: Mon Apr 11, 2005 5:55 am
Location: Iowa - USA


Return to NeoBook Knowledgebase

Who is online

Users browsing this forum: No registered users and 1 guest