Jump to content
Sign in to follow this  
Richard Robertson

IP validation

Recommended Posts

I was writing a program, and figured this might be useful to someone. It is a function that validates a string to see if it contains a valid IP address.

Func IsIPAddress($text)
    Return StringRegExp($text, "(((25[0-5])|(2[0-4][0-9])|(1[0-9][0-9])|([1-9]?[0-9]))\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9][0-9])|([1-9]?[0-9]))")
EndFunc

It returns a 0 (false) if it is not an IP address, and a 1 (true) if it is valid. I know regular expressions confuse people, so I figured I'd share one that I got working.

Edited by Richard Robertson

Share this post


Link to post
Share on other sites

the func is ok :) but it dosen't take in consideration all the facts :(

here is something i did a while ago and it checks for more stuff(everything needed i think :D)

Func _isip($ip,$P_strict=0)
    $t_ip=$ip
    $port=StringInStr($t_ip,":");check for : (for the port)
    If $port Then ;has a port attached
        $t_ip=StringLeft($ip,$port-1);remove the port from the rest of the ip
        If $P_strict Then ;return 0 if port is wrong
            $zport=Int(StringTrimLeft($ip,$port));retrieve the port
            If $zport>65000 Or $zport<0 Then Return 0;port is wrong
        EndIf
    EndIf
    $zip=StringSplit($t_ip,".")
    If $zip[0]<>4  Then Return 0;incorect number of segments
    If Int($zip[1])>255 Or Int($zip[1])<1 Then Return 0;xxx.ooo.ooo.ooo
    If Int($zip[2])>255 Or Int($zip[1])<0 Then Return 0;ooo.xxx.ooo.ooo
    If Int($zip[3])>255 Or Int($zip[3])<0 Then Return 0;ooo.ooo.xxx.ooo
    If Int($zip[4])>255 Or Int($zip[4])<0 Then Return 0;ooo.ooo.ooo.xxx
    $bc=1 ; is it 255.255.255.255 ?
    For $i=1 To 4
        If $zip[$i]<>255 Then $bc=0;no
        ;255.255.255.255 can never be a ip but anything else that ends with .255 can be
        ;ex:10.10.0.255 can actually be an ip address and not a broadcast address
    Next
    If $bc Then Return 0;a broadcast address is not really an ip address...
    If $zip[4]=0 Then;subnet not ip
        If $port Then
            Return 0;subnet with port?
        Else
            Return 2;subnet
        EndIf
    EndIf
    Return 1;;string is a ip
EndFunc

edit:comments

Edited by TheMadman

Only two things are infinite, the universe and human stupidity, and i'm not sure about the former -Alber EinsteinPractice makes perfect! but nobody's perfect so why practice at all?http://forum.ambrozie.ro

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...