|
-
Oct 24th, 2012, 03:22 PM
#1
Thread Starter
Fanatic Member
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:
Public Function IsIPAddress(ByVal Source As String) As Boolean
Dim Temp() As String = Source.Split(".")
Dim Slots(3) As Integer
Dim Vaild As Boolean = True
'Must be 3 pairs
If ((Temp.Count - 1) = 3) Then
Try
'Get ip pairs
Slots(0) = Integer.Parse(Temp(0))
Slots(1) = Integer.Parse(Temp(1))
Slots(2) = Integer.Parse(Temp(2))
Slots(3) = Integer.Parse(Temp(3))
'Check for vaild numbers in range of 0..255
For x As Integer = 0 To Slots.Count - 1
'Check if less than zero.
If Slots(x) < 0 Then
Vaild = False
'Check if more than 255
ElseIf Slots(x) > 255 Then
Vaild = False
End If
Next x
Catch ex As Exception
Return False
End Try
Else
'Set flag falue.
Return False
End If
Return Vaild
End Function
Private Sub cmdTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdTest.Click
Dim Viald As Boolean = False
'Test 1
Viald = IsIPAddress("127.0.0.1") 'Vaild
MessageBox.Show(Viald, "IP-TEST", MessageBoxButtons.OK, MessageBoxIcon.Information)
'Test 2
Viald = IsIPAddress("256.0.0.1") 'Invaild
MessageBox.Show(Viald, "IP-TEST", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
-
Oct 25th, 2012, 11:45 AM
#2
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.
-
Oct 25th, 2012, 12:59 PM
#3
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
-
Oct 26th, 2012, 01:30 AM
#4
Thread Starter
Fanatic Member
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.
-
Oct 26th, 2012, 08:12 AM
#5
Re: Is IP Addres Vaild
I am not a fan of System.Net.IPAddress.TryParse for the reason shown in the code I posted.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|