GLBasic forum

Codesnippets => Code Snippets => Topic started by: MrTAToad on 2009-Nov-26

Title: Validate IPV4 address
Post by: MrTAToad on 2009-Nov-26
Quick routine to validate an IPV4 address :

Code (glbasic) Select
FUNCTION validateIP4Address%:ip$
LOCAL section$[]
LOCAL loop$

DIM section$[0]

IF SPLITSTR(ip$,section$[],".")<>4 THEN RETURN FALSE

FOREACH loop$ IN section$[]
IF LEN(loop$)<1 OR LEN(loop$)>3 THEN RETURN FALSE
IF isNumber(loop$)=FALSE THEN RETURN FALSE
NEXT

RETURN TRUE
ENDFUNCTION

FUNCTION isNumber%:text$
LOCAL loop%
LOCAL one$

FOR loop%=0 TO LEN(text$)-1
one$=MID$(text$,loop%,1)
IF one$<"0" OR one$>"9" THEN RETURN FALSE
NEXT

loop%=INTEGER(text$)
IF loop%<0 OR loop%>255 THEN RETURN FALSE

RETURN TRUE
ENDFUNCTION
Title: Re: Validate IPV4 address
Post by: MrTAToad on 2009-Nov-27
Would like to find decent IPV6 validation code, but there is nothing simple around at the moment.
Title: Re: Validate IPV4 address
Post by: Moru on 2009-Nov-27
No problems, here are the RFC. (not sure if it's the newest...)  ;)

http://www.faqs.org/rfcs/rfc2373.html
Title: Re: Validate IPV4 address
Post by: MrTAToad on 2009-Nov-28
Trouble is, there are allowed shortcuts for the different sections :(
Title: Re: Validate IPV4 address
Post by: Moru on 2009-Nov-28
Yes, there was supposed to be an ironic smile up there somewhere... :-)
Title: Re: Validate IPV4 address
Post by: MrTAToad on 2009-Nov-29
True :)