cliff_m
asked on
Validate an IP address
how do you validate an IP address. I know that the range is from 0.0.0.0 to 255.255.255.255. I've tried Case "0.0.0.0" to "255.255.255.255" but it allows values of 255.444.555.66 and not 26.23.21.7. Any help would be appreciated.
Why don't you split it up?
select case does not work, because you are attempting to validate a string. anything that starts with 255 will fall within the range. This is what you need to do.
1) break the ip address up into it's 4 components (separate the string based on the
public sub IPComponents(szIP as string, nIP() as integer)
dim nI as integer
dim nJ as integer
dim nCt as integer
redim nIP(0 to 3) as integer
nI = instr(szip, ".")
nj = 1
do until nI = 0
nIP(nct) = cint(mid$(szip, nj, ni - nj))
nct = nct + 1
nj = ni
ni = instr(ni + 1, szip, ".")
loop
end sub
Now validate the 4 components
dim nIP() as integer
call ipcomponents("255.444.555. 66", nip())
for nI = 0 to 3
if nip(ni) < 0 or nip(ni) > 255 then
msgbox "Not valid"
end if
next ni
1) break the ip address up into it's 4 components (separate the string based on the
public sub IPComponents(szIP as string, nIP() as integer)
dim nI as integer
dim nJ as integer
dim nCt as integer
redim nIP(0 to 3) as integer
nI = instr(szip, ".")
nj = 1
do until nI = 0
nIP(nct) = cint(mid$(szip, nj, ni - nj))
nct = nct + 1
nj = ni
ni = instr(ni + 1, szip, ".")
loop
end sub
Now validate the 4 components
dim nIP() as integer
call ipcomponents("255.444.555.
for nI = 0 to 3
if nip(ni) < 0 or nip(ni) > 255 then
msgbox "Not valid"
end if
next ni
ASKER
I have tried this code as is and it apparently does not reject the test ip address "255.444.555.66". Perhaps I am doing it wrong. Also I try following the code and I think I understand it for the most part, however I am confused by the nct in the following code:
nIP(nct) = cint(mid$(szip, nj, ni - nj))
nct = nct + 1
what function does it have?
Please forgive my ignorance, for I am a vb newbie.
Cliff Martin
nIP(nct) = cint(mid$(szip, nj, ni - nj))
nct = nct + 1
what function does it have?
Please forgive my ignorance, for I am a vb newbie.
Cliff Martin
ASKER
I have tried this code as is and it apparently does not reject the test ip address "255.444.555.66". Perhaps I am doing it wrong. Also I try following the code and I think I understand it for the most part, however I am confused by the nct in the following code:
nIP(nct) = cint(mid$(szip, nj, ni - nj))
nct = nct + 1
what function does it have?
Please forgive my ignorance, for I am a vb newbie.
Cliff Martin
nIP(nct) = cint(mid$(szip, nj, ni - nj))
nct = nct + 1
what function does it have?
Please forgive my ignorance, for I am a vb newbie.
Cliff Martin
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.