Results 1 to 5 of 5

Thread: Is IP Addres Vaild

  1. #1
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 10
    Location
    Wales UK
    Posts
    540

    Is IP Addres Vaild

    Hi I needed a function to check if a IP was valid and came up with this little bit of code, It's not perfect but seems to do what I needed it for
    anyway I left it here as I thought it maybe of some use to someone else. Anyway comments and suggestions welcome.

    vbnet Code:
    1. Public Function IsIPAddress(ByVal Source As String) As Boolean
    2.         Dim Temp() As String = Source.Split(".")
    3.         Dim Slots(3) As Integer
    4.         Dim Vaild As Boolean = True
    5.  
    6.         'Must be 3 pairs
    7.         If ((Temp.Count - 1) = 3) Then
    8.             Try
    9.                 'Get ip pairs
    10.                 Slots(0) = Integer.Parse(Temp(0))
    11.                 Slots(1) = Integer.Parse(Temp(1))
    12.                 Slots(2) = Integer.Parse(Temp(2))
    13.                 Slots(3) = Integer.Parse(Temp(3))
    14.                 'Check for vaild numbers in range of 0..255
    15.                 For x As Integer = 0 To Slots.Count - 1
    16.                     'Check if less than zero.
    17.                     If Slots(x) < 0 Then
    18.                         Vaild = False
    19.                         'Check if more than 255
    20.                     ElseIf Slots(x) > 255 Then
    21.                         Vaild = False
    22.                     End If
    23.                 Next x
    24.             Catch ex As Exception
    25.                 Return False
    26.             End Try
    27.         Else
    28.             'Set flag falue.
    29.             Return False
    30.         End If
    31.  
    32.         Return Vaild
    33.     End Function
    34.  
    35.     Private Sub cmdTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdTest.Click
    36.         Dim Viald As Boolean = False
    37.         'Test 1
    38.         Viald = IsIPAddress("127.0.0.1") 'Vaild
    39.         MessageBox.Show(Viald, "IP-TEST", MessageBoxButtons.OK, MessageBoxIcon.Information)
    40.         'Test 2
    41.         Viald = IsIPAddress("256.0.0.1") 'Invaild
    42.         MessageBox.Show(Viald, "IP-TEST", MessageBoxButtons.OK, MessageBoxIcon.Information)
    43.     End Sub

  2. #2
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 11
    Location
    South Africa
    Posts
    865

    Re: Is IP Addres Vaild

    System.Net.IPAddress.TryParse() ?
    But it's still good to know how to do these things by hand. I rewrote the sin function once just to understand how it worked.

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 07
    Location
    Pointless Forest 38.517,-92.023
    Posts
    7,215

    Re: Is IP Addres Vaild

    FYI:

    Code:
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Debug.WriteLine("")
            Debug.WriteLine(IsIPv4Address("192.168.255.0"))
            Debug.WriteLine(IsIPv4Address("192.168.255")) 'note that this parses
            Debug.WriteLine(IsIPv4Address("192.168.255.256"))
        End Sub
    
        Public Function IsIPv4Address(ByVal ipStr As String) As Boolean
            Debug.WriteLine(">>> " & ipStr)
            'using System.Net.IPAddress.TryParse method
            Dim ipAddr As System.Net.IPAddress = Nothing
            If System.Net.IPAddress.TryParse(ipStr, ipAddr) Then
                Debug.WriteLine(ipAddr.ToString)
            Else
                Debug.WriteLine("Parse failed")
            End If
    
            'check each octet method
            Dim Temp() As String = ipStr.Split("."c)
            Dim isValid As Boolean = True
            If Temp.Count = 4 Then
                For Each s As String In Temp
                    Dim octet As Integer
                    If Not (Integer.TryParse(s, octet) AndAlso octet >= 0 AndAlso octet <= 255) Then
                        isValid = False
                        Exit For
                    End If
                Next
            Else
                isValid = False
            End If
            Return isValid
        End Function
    My First Computer --- Documentation Link (RT?M) --- Using the Debugger ---
    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein
    "They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety." Benjamin Franklin

  4. #4
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 10
    Location
    Wales UK
    Posts
    540

    Re: Is IP Addres Vaild

    Did not know about that System.Net.IPAddress.TryParse() BlindSniper will ahve to have a look
    lol yer I like re-inventing the wheel to. I always like to see how something works.
    Nice bit of code dbasnett will come in handy.

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 07
    Location
    Pointless Forest 38.517,-92.023
    Posts
    7,215

    Re: Is IP Addres Vaild

    I am not a fan of System.Net.IPAddress.TryParse for the reason shown in the code I posted.
    My First Computer --- Documentation Link (RT?M) --- Using the Debugger ---
    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein
    "They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety." Benjamin Franklin

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •