Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Validating ip address

Validating ip address

Scheduled Pinned Locked Moved C#
question
11 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sarang_k
    wrote on last edited by
    #1

    Hi, In windows application how can i validate the ip address in a textbox on key press event. I have used masked text box but still no solution. Can anyone give me a solution ? Thanks in advance.

    P P R 3 Replies Last reply
    0
    • S sarang_k

      Hi, In windows application how can i validate the ip address in a textbox on key press event. I have used masked text box but still no solution. Can anyone give me a solution ? Thanks in advance.

      P Offline
      P Offline
      Pravin Patil Mumbai
      wrote on last edited by
      #2

      You can use the regular expression validator to validate the IP address in the text box. Check out : 1. Validation with Regular Expressions Made Simple[^] 2. http://www.codeproject.com/search.aspx?q=regular+expression+validator&x=0&y=0&sbo=kw[^] IP Address regular expression : \b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b

      I quit being afraid when my first venture failed and the sky didn't fall down.

      P 1 Reply Last reply
      0
      • S sarang_k

        Hi, In windows application how can i validate the ip address in a textbox on key press event. I have used masked text box but still no solution. Can anyone give me a solution ? Thanks in advance.

        P Offline
        P Offline
        Peter_in_2780
        wrote on last edited by
        #3

        Use a regular expression, such as

        ^(2[0-4]\d|25[0-5]|[01]?\d\d?)\.(2[0-4]\d|25[0-5]|[01]?\d\d?)\.(2[0-4]\d|25[0-5]|[01]?\d\d?)\.(2[0-4]\d|25[0-5]|[01]?\d\d?)$

        (wrap it in whatever delimiters you need.) If you can't read this, get a copy of Expresso and paste the expression into the expression window. [edit]I thought it was broken, but it's not... It's just smarter than I am feeling right now. :-O Need coffee! ;P Forget the edit.[/edit] Peter

        Software rusts. Simon Stephenson, ca 1994.

        1 Reply Last reply
        0
        • P Pravin Patil Mumbai

          You can use the regular expression validator to validate the IP address in the text box. Check out : 1. Validation with Regular Expressions Made Simple[^] 2. http://www.codeproject.com/search.aspx?q=regular+expression+validator&x=0&y=0&sbo=kw[^] IP Address regular expression : \b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b

          I quit being afraid when my first venture failed and the sky didn't fall down.

          P Offline
          P Offline
          Peter_in_2780
          wrote on last edited by
          #4

          Your regex is broken (but then so is mine - I'm about to fix it and explain why). You don't want \b before and after. Also, yours would accept 345.567.678.789, which is not a valid IP address! Peter

          Software rusts. Simon Stephenson, ca 1994.

          P 1 Reply Last reply
          0
          • P Peter_in_2780

            Your regex is broken (but then so is mine - I'm about to fix it and explain why). You don't want \b before and after. Also, yours would accept 345.567.678.789, which is not a valid IP address! Peter

            Software rusts. Simon Stephenson, ca 1994.

            P Offline
            P Offline
            Pravin Patil Mumbai
            wrote on last edited by
            #5

            I agree with you, my regex would accept 999.999.999.999 as well. I will update that. Thanks....

            I quit being afraid when my first venture failed and the sky didn't fall down.

            1 Reply Last reply
            0
            • S sarang_k

              Hi, In windows application how can i validate the ip address in a textbox on key press event. I have used masked text box but still no solution. Can anyone give me a solution ? Thanks in advance.

              R Offline
              R Offline
              realJSOP
              wrote on last edited by
              #6

              Regex is NOT necessary. Just use IPAddress.Parse(myIPAddress), and handle the FormatException exception.

              ".45 ACP - because shooting twice is just silly" - JSOP, 2010
              -----
              You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
              -----
              "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997

              P R 2 Replies Last reply
              0
              • R realJSOP

                Regex is NOT necessary. Just use IPAddress.Parse(myIPAddress), and handle the FormatException exception.

                ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                -----
                You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                -----
                "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #7

                And there we have the correct answer. Regexes are good - when used in appropriate cases. Reinventing the wheel is not the case though.

                Forgive your enemies - it messes with their heads

                My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                1 Reply Last reply
                0
                • R realJSOP

                  Regex is NOT necessary. Just use IPAddress.Parse(myIPAddress), and handle the FormatException exception.

                  ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                  -----
                  You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                  -----
                  "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997

                  R Offline
                  R Offline
                  RichardGrimmer
                  wrote on last edited by
                  #8

                  Oh John....you wouldn't be using exception handling as program flow control now would you? Naughty boy - go sit in the corner... Perhaps using IPAddress.TryParse() would be less "expensive"?

                  C# has already designed away most of the tedium of C++.

                  J R B 3 Replies Last reply
                  0
                  • R RichardGrimmer

                    Oh John....you wouldn't be using exception handling as program flow control now would you? Naughty boy - go sit in the corner... Perhaps using IPAddress.TryParse() would be less "expensive"?

                    C# has already designed away most of the tedium of C++.

                    J Offline
                    J Offline
                    jschell
                    wrote on last edited by
                    #9

                    The cost in a GUI (per the OP) would be difficult to measure and certainly not significant. Actually I am not sure what sort of application it would take where this would be expensive. It would require all of the following. 1. IPs arrive as strings. 2. A signficant number are invalid. 3. There are a lot of them.

                    1 Reply Last reply
                    0
                    • R RichardGrimmer

                      Oh John....you wouldn't be using exception handling as program flow control now would you? Naughty boy - go sit in the corner... Perhaps using IPAddress.TryParse() would be less "expensive"?

                      C# has already designed away most of the tedium of C++.

                      R Offline
                      R Offline
                      realJSOP
                      wrote on last edited by
                      #10

                      I merely mentioned it to avoid being pinged for not doing so. It seems one cannot win. :) Besides that, I'm sure intellisense works in his copy of VS, so he may have discovered it on his own.

                      ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                      -----
                      You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                      -----
                      "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997

                      1 Reply Last reply
                      0
                      • R RichardGrimmer

                        Oh John....you wouldn't be using exception handling as program flow control now would you? Naughty boy - go sit in the corner... Perhaps using IPAddress.TryParse() would be less "expensive"?

                        C# has already designed away most of the tedium of C++.

                        B Offline
                        B Offline
                        BobJanova
                        wrote on last edited by
                        #11

                        Depending on the situation, it may be appropriate (i.e. if the vast majority are valid and an invalid one really is exceptional). For UI validation it frankly doesn't matter but from a perspective of style you are correct.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups