Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 42215

Is IP Addres Vaild

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

Viewing all articles
Browse latest Browse all 42215

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>