Jump to content

Check if ip valid from input


 Share

Go to solution Solved by BrewManNH,

Recommended Posts

i have make this for test if the ip is valid or not from input

any one see something messing please tell me

any one have a better code tell me because im making a program and i dont need to enter a wrong ip address in me database

here is my code

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
$Form1 = GUICreate("Form1", 193, 119, 357, 225)
$Input1 = GUICtrlCreateInput("", 24, 30, 145, 21)
$Button1 = GUICtrlCreateButton("Button1", 48, 64, 97, 33)
GUISetState(@SW_SHOW)
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        case $Button1
            $i = Checkip(GUICtrlRead($Input1))
            MsgBox("","",$i)
    EndSwitch
WEnd


Func Checkip($data)
        $split = StringSplit($data,".",1)
        $1stnum = "*"
        $2dnum = "*"
        $3dnum = "*"
        $4thnum = "*"
        if $split[0] = "4" Then
            $0 = StringReplace($split[1],"0","")
            $1 = StringReplace($0,"1","")
            $2 = StringReplace($1,"2","")
            $3 = StringReplace($2,"3","")
            $4 = StringReplace($3,"4","")
            $5 = StringReplace($4,"5","")
            $6 = StringReplace($5,"6","")
            $7 = StringReplace($6,"7","")
            $8 = StringReplace($7,"8","")
            $8 = StringReplace($8,"9","")
            $1stnum = $8
            $0 = StringReplace($split[2],"0","")
            $1 = StringReplace($0,"1","")
            $2 = StringReplace($1,"2","")
            $3 = StringReplace($2,"3","")
            $4 = StringReplace($3,"4","")
            $5 = StringReplace($4,"5","")
            $6 = StringReplace($5,"6","")
            $7 = StringReplace($6,"7","")
            $8 = StringReplace($7,"8","")
            $8 = StringReplace($8,"9","")
            $2dnum = $8
            $0 = StringReplace($split[3],"0","")
            $1 = StringReplace($0,"1","")
            $2 = StringReplace($1,"2","")
            $3 = StringReplace($2,"3","")
            $4 = StringReplace($3,"4","")
            $5 = StringReplace($4,"5","")
            $6 = StringReplace($5,"6","")
            $7 = StringReplace($6,"7","")
            $8 = StringReplace($7,"8","")
            $8 = StringReplace($8,"9","")
            $3dnum = $8
            $0 = StringReplace($split[4],"0","")
            $1 = StringReplace($0,"1","")
            $2 = StringReplace($1,"2","")
            $3 = StringReplace($2,"3","")
            $4 = StringReplace($3,"4","")
            $5 = StringReplace($4,"5","")
            $6 = StringReplace($5,"6","")
            $7 = StringReplace($6,"7","")
            $8 = StringReplace($7,"8","")
            $8 = StringReplace($8,"9","")
            $4thnum = $8
            if $1stnum <> "" Then
                return "Please Enter an Correct IP address Found string in [ ???.***.***.*** ]"
            ElseIf $2dnum <> "" Then
                return "Please Enter an Correct IP address Found string in [ ***.???.***.*** ]"
            ElseIf $3dnum <> "" Then
                return "Please Enter an Correct IP address Found string in [ ***.***.???.*** ]"
            ElseIf $4thnum <> "" Then
                return "Please Enter an Correct IP address Found string in [ ***.***.***.??? ]"
            ElseIf $1stnum = "" and $2dnum = "" and $3dnum = "" and $4thnum = "" Then
                if $split[1] > 254 Then
                    return "Please Enter an number less than 255 [ ???.***.***.*** ]"
                ElseIf $split[2] > 254 Then
                    return "Please Enter an number less than 255 [ ***.???.***.*** ]"
                ElseIf $split[3] > 254 Then
                    return "Please Enter an number less than 255 [ ***.***.???.*** ]"
                ElseIf $split[4] > 254 Then
                    return "Please Enter an number less than 255 [ ***.***.***.??? ]"
                Else
                    $len1 = StringLen($split[1])
                    $len2 = StringLen($split[2])
                    $len3 = StringLen($split[3])
                    $len4 = StringLen($split[4])
                    If $len1 > 1 and StringLeft($split[1],1) = "0" Then
                        return "Please Enter an Correct number [ 0??.***.***.*** ]"
                    ElseIf $len2 > 1 and StringLeft($split[2],1) = "0" Then
                        return "Please Enter an Correct number [ ***.0??.***.*** ]"
                    ElseIf $len3 > 1 and StringLeft($split[3],1) = "0" Then
                        return "Please Enter an Correct number [ ***.***.0??.*** ]"
                    ElseIf $len4 > 1 and StringLeft($split[4],1) = "0" Then
                        return "Please Enter an Correct number [ ***.***.***.0?? ]"
                    Else
                        Return True
                    EndIf
                EndIf
            EndIf
        Else
            MsgBox("","","Please Enter an Correct IP address Like [ ***.***.***.*** ]")
        EndIf
EndFunc
Link to comment
Share on other sites

  • Solution

Here's something I came up with a while ago that might help.

 

; FUNCTION# ===========================================================================================================
; Name...........: _ValidIP
; Description ...: Verifies whether an IP address is a valid IPv4 address or not
; Syntax.........: _ValidIP($sIP)
; Parameters ....: $sIP - IP address to validate
;
; Return values .: Success - Array containing split IP Address, IP address in Hex, and the Class of the IP address
;                            array[0] - [3] = the IP address split into octets
;                            array[4]       = IP address in Hex
;                            array[5]       = Class of the IP address [A through D]
;                  Failure - -1, sets @error
;                  |1 - IP address starts with an invalid number = 0, 127 , 169 or is > 239
;                  |2 - one of the octets of the IP address is out of the range 0-255 or contains invalid characters
;                  |3 - IP Address is not a valid dotted IP address (ex. valid address 190.40.100.20)
;                  |4 - Last octet ends in 0 or 255 which are invalid for an IP address
; Author ........: BrewManNH
; Modified.......:
; Remarks .......: This will accept an IP address that is 4 octets long, and contains only numbers and falls within
;                  valid IP address values. Class A networks can't start with 0 or 127. 169.xx.xx.xx is reserved and is
;                  invalid and any address that starts above 239, ex. 240.xx.xx.xx is reserved. The address range
;                  224-239 1s reserved as well for Multicast groups but can be a valid IP address range if you're using
;                  it as such. Any IP address ending in 0 or 255 is also invalid for an IP
; Related .......:
; Link ..........:
; Example .......: Yes
; =====================================================================================================================
Func _ValidIP($sIP)
    Local $adIPAddressInfo[6]
 
    Local $aArray = StringSplit($sIP, ".", 2)
 
    If Not IsArray($aArray) Or UBound($aArray) <> 4 Then Return SetError(3, 0, -1)
 
    Local $dString = "0x"
 
    If $aArray[0] <= 0 Or $aArray[0] > 239 Or $aArray[0] = 127 Or $aArray[0] = 169 Then
        Return SetError(1, 0, -1)
    EndIf
 
    For $I = 0 To 3
        If $I < 3 Then
            If $aArray[$I] < 0 Or $aArray[$I] > 255 Or Not StringIsDigit($aArray[$I]) Then
                Return SetError(2, 0, -1)
            EndIf
        Else
            If Not StringIsDigit($aArray[$I]) Then
                Return SetError(2, 0, -1)
            EndIf
 
            If $aArray[$I] < 1 Or $aArray[$I] > 254 Then
                Return SetError(4, 0, -1)
            EndIf
        EndIf
 
        $dString &= StringRight(Hex($aArray[$I]), 2)
 
        $adIPAddressInfo[$I] = $aArray[$I]
    Next
 
    $adIPAddressInfo[4] = $dString
 
    Switch $aArray[0]
        Case 1 To 126
            $adIPAddressInfo[5] = "A"
            Return $adIPAddressInfo
        Case 128 To 191
            $adIPAddressInfo[5] = "B"
            Return $adIPAddressInfo
        Case 192 To 223
            $adIPAddressInfo[5] = "C"
            Return $adIPAddressInfo
        Case 224 To 239
            $adIPAddressInfo[5] = "D"
            Return $adIPAddressInfo
    EndSwitch
EndFunc   ;==>_ValidIP

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

You can find a couple others like it on this page. I wouldn't use the _IsIP one though if you're trying to determine if it's a valid IP, as that one only determines whether or not you sent 4 digits separated by dots that are all between 0 and 255.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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...