We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Validate an IP address

cliff_m
cliff_m asked
on
Medium Priority
381 Views
Last Modified: 2010-04-30
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.
Comment
Watch Question

Commented:
Why don't you split it up?

Commented:
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
               

Author

Commented:
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

Author

Commented:
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
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.