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