Sign in to follow this  
Followers 0
Jango

Is IP address valid

13 posts in this topic

#1 ·  Posted (edited)

Hi,

I ve made extensive search on the forum but i have not found what i'm looking for. I would like to check if a string is a valide IP address can someone help me or give me a link to a post talking about that ?

Cu

Edit: Sorry i just noticed i posted in the wrong section, can a mod move my topic thank you and sorry again

Edited by Jango

Share this post


Link to post
Share on other sites



I believe this should do it:

Func _StringisIpAndValid($zIp)
    $t_ip = StringSplit($zIp, ".")
    If $t_ip[0] = 4 And StringIsDigit($t_ip[1]) And StringIsDigit($t_ip[2]) And StringIsDigit($t_ip[3]) And StringIsDigit($t_ip[4]) And $t_ip[1] <= 255 And $t_ip[2] <= 255 And $t_ip[3] <= 255 And $t_ip[4] <= 255 Then Return True
    Return False
EndFunc

My Scripts:Radioblog Club Music DownloaderOther stuff:Fun movieIm serious read the help file it helps :PFight 'Till you drop. Never stop, You Cant give up. Til you reach the top Fight! you’re the best in town Fight!

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Maybe you should use RegularExpressions:

Tutorial if you need one (even with a tut on how to check for a valid IP Adress)

http://www.regular-expressions.info/examples.html

Edited by XxXFaNtA

Share this post


Link to post
Share on other sites

I believe this should do it:

Func _StringisIpAndValid($zIp)
    $t_ip = StringSplit($zIp, ".")
    If $t_ip[0] = 4 And StringIsDigit($t_ip[1]) And StringIsDigit($t_ip[2]) And StringIsDigit($t_ip[3]) And StringIsDigit($t_ip[4]) And $t_ip[1] <= 255 And $t_ip[2] <= 255 And $t_ip[3] <= 255 And $t_ip[4] <= 255 Then Return True
    Return False
EndFunc

Thank you rambo it is exactly what i'm looking for. Do you made it yourself ?

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Your funciton has an error in it. Example: if $zIP = -127.0.0.-1 since -127 is, by definition less than 255, the function will still return true.

Edited by Blue_Drache

Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache

Share this post


Link to post
Share on other sites

MsgBox(0,"", "Valid (Returns 0): " & _IsValidIP("192.168.0.1"))
MsgBox(0,"", "InValid (Retruns 1): " & _IsValidIP("168.0.1"))
MsgBox(0,"", "InValid (Retruns 2): " & _IsValidIP("266.168.0.1"))


Func _IsValidIP($theIp)
    $theArray = StringSplit($theIp, ".")
    If $theArray[0] <> 4 Then Return 1
    For $X = 1 to $theArray[0]
        If $theArray[$X] < 0 OR $theArray[$X] > 255 Then Return 2
    Next
    Return 0
EndFunc

Share this post


Link to post
Share on other sites

What if my IP is IPv6? <_<

Share this post


Link to post
Share on other sites

"Build a better error-trap and the programmers will beat down your door"


Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

MsgBox(0,"", "Valid (Returns 1): " & _IsValidIPv6("FEDC:BA98:7654:3210:FEDC:BA98:7654:3210"))
MsgBox(0,"", "InValid (Returns 0): " & _IsValidIPv6("FEDC::7654:3210::BA98:7654:3210"))

Func _IsValidIPv6($theIp)
    $pattern = "^((([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}:[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)?[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){4}:([0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){3}"
    $pattern &= ":([0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){2}:([0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(([0-9A-Fa-f]{1,4}:){0,5}:((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}"
    $pattern &= "(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(::([0-9A-Fa-f]{1,4}:){0,5}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|([0-9A-Fa-f]{1,4}::([0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})|(::([0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){1,7}:))$"

    $result = StringRegExp($theIp,$pattern)
    Return $result
EndFunc

I tried posting this with AutoIt tags but it said I had too many emoticons!

I think I just broke the forum.

Edited by weaponx

Share this post


Link to post
Share on other sites

Lol well done IP Board does'nt like too mucha emoticons

Share this post


Link to post
Share on other sites

IP.Board hates emos. *LOL*


Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache

Share this post


Link to post
Share on other sites

IP.Board hates emos. *LOL*

Oh...I'm gonna go slit my wrists and flock of seagalls my hair...

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  
Followers 0