Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validate IP Addresses

Status
Not open for further replies.

dahoule

Programmer
Sep 27, 2001
4
US
I am writing an application that needs to do some validation on user-entered IP addresses. I took a stab at writing my own code, but figured somebody must have doen something like this before. I need to check the following:

- there are 4 octets seperated by 3 periods (.)
- each octet must be between 1 and 255

Here's the beginning of what I wrote with msgbox's that show me what each octet is.

Private Sub Split_IP()
Dim strIP As String
Dim pos As Long
Dim pos2 As Integer
Dim first_oct As Integer
Dim second_oct As Integer
Dim third_oct As Integer
Dim length As Integer

txtIP.SetFocus
strIP = txtIP.Text

pos = InStr(strIP, ".")
first_oct = Left(strIP, pos)
MsgBox "first_oct = " & first_oct & ""

pos = pos + 1

second_oct = Mid(strIP, pos, (InStr([pos], strIP, ".") - pos))
MsgBox "second_oct = " & second_oct & ""

pos = pos + Len(second_oct)

third_oct = Mid(strIP, pos, (InStr([pos], strIP, ".")))
MsgBox "third_oct = " & third_oct & ""


End Sub

This works up until the 3rd octet. Anyone have any suggestions?

Thanks in advance!!

Don
 
How about,

Private Sub Split_IP()
Dim strIP As String
Dim pos As Long
Dim pos2 As Integer
Dim first_oct As Integer
Dim second_oct As Integer
Dim third_oct As Integer
Dim length As Integer

txtIP.SetFocus
strIP = txtIP.Text

pos = InStr(strIP, ".")
first_oct = Left(strIP, pos)
MsgBox "first_oct = " & first_oct & ""

pos = pos + 1

second_oct = Mid(strIP, pos, (InStr([pos], strIP, ".") - pos))
MsgBox "second_oct = " & second_oct & ""

pos = pos + Len(second_oct)

third_oct = right$(strIP,len(strIP)-pos-1)


End Sub

Nick
 
I will mention that in some earlier post, I saw someone having trouble with storing the IP as one text field. Can't remember what the problem was, but I think it was strongly suggested that they store them as four different fields. I think it might have been a sorting issue. Text strings don't sort like numbers do. Terry M. Hoey
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top