Take the 2-minute tour ×

How can I use a MaskedTextBox to prevent the user from entering an invalid IP address? (I want it to behave just like the Windows one).

share|improve this question
Why not just read the value in and save the user some trouble? Never make a user do what you can do for them. –  Woot4Moo Oct 28 '11 at 1:00
add comment

4 Answers

up vote 5 down vote accepted

Try this:

IPAddress ipAddress;
if (IPAddress.TryParse(maskedTextBoxY.Text, out ipAddress))
    //valid ip
    //is not valid ip

note: to use it, you need import the System.Net namespace:

using System.Net;
share|improve this answer
System.Net isn't a DLL, it's a namespace. Just a note. –  minitech Oct 29 '11 at 0:47
You're right. edited answer. –  Kakashi Oct 30 '11 at 0:21
add comment

It is better to use REGEX to validate user input. Here's an example:


        string ip1 = "";
        string ip2 = "302.0.0.1";

        Regex.IsMatch(ip1, pattern); // returns true
        Regex.IsMatch(ip2, pattern); // returns false
share|improve this answer
and now you have two problems... –  Woot4Moo Oct 28 '11 at 0:53
Um, since when? This doesn't answer the question at all. -1 –  minitech Oct 28 '11 at 0:54
@minitech - I believe Woot4Moo is referring to that old saw about regexes - see codinghorror.com/blog/2008/06/… –  Will Chesterfield Oct 28 '11 at 0:58
@WillChesterfield: I was referring to Dan's post, not Woot4Moo's comment. System.Net.IPAddress.TryParse is far better. –  minitech Oct 28 '11 at 1:32
add comment

Much simpler than the other answers:

Use System.Net.IPAddress and System.Windows.Forms.MaskedTextBox

set the following properties of the MaskedTextBox: MaskedTextBox.Mask = ###.###.###.### MaskedTextBox.ValidatingType = typeof(System.Net.IPAddress);

whenever the text box is validating, event MaskedTextBox.TypeValidationCompleted is raised. The event arguments show the following:

  • Is the typed text acceptable as an IP address? (= does System.Net.IPAddress.TryParse return ok)
  • Description of the error as a string
  • The parsed value (= an object of System.NET.IpAddress
  • The type of the parsed value. Needed if you have several MaskedTextBoxes with different masks

Upon receipt of this event you can decide whether to use the value or notify the user what is wrong with the value.

share|improve this answer
add comment

Set the mask to: ###.###.###.###

Will display like this:

enter image description here

share|improve this answer
and what of IPv6? –  Woot4Moo Oct 28 '11 at 0:59
@Woot4Moo I don't think can be done with a single mask. You'll need to listen for key pressed and handle wich characters are/aren't allowed –  Icarus Oct 28 '11 at 1:04
Correct, MaskedTextBox does not allow you to do complex validations directly. You can use KeyPress event to read user input and determine in it is valid. And a right Regex expression does help to do this task. –  Dan Oct 28 '11 at 1:07
@Icarus good to know, thanks –  Woot4Moo Oct 28 '11 at 1:08
add comment

Your Answer


