Validating ip address
-
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.
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.
-
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.
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.
-
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.
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.
-
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.
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.
-
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.
Regex is NOT necessary. Just use
IPAddress.Parse(myIPAddress)
, and handle theFormatException
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 -
Regex is NOT necessary. Just use
IPAddress.Parse(myIPAddress)
, and handle theFormatException
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, 1997And 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
-
Regex is NOT necessary. Just use
IPAddress.Parse(myIPAddress)
, and handle theFormatException
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, 1997Oh 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++.
-
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++.
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.
-
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++.
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 -
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++.