Jump to content

Is IP address valid


Jango
 Share

Recommended Posts

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
Link to comment
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!
Link to comment
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 ?

Link to comment
Share on other sites

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

Link to comment
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

Link to comment
Share on other sites

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
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...