IPv4 validation with RegEx

If you’re developing an application that uses network communications, it could be very useful, if you can validate every IPv4 address that users may enter on a form. Here’s a small code with winform application to validate both IP address, network mask addresses. I’m using 2 different regular expressions to validate these 2 different type of addresses. You’ll only need 2 TextBoxes and 2 small images (a green tick and a red cross).

You need to create 2 events for both TextBoxes: Leave and TextChanged events.

private void txbIPaddress_TextChanged(object sender, EventArgs e)
        {
            if (txbIPaddress.Text.Length == 0)
            {
                pictureBox1.Image = null;
            }
            else
            {
                string pattern = @"\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b";
                if (!Regex.IsMatch(txbIPaddress.Text, pattern))
                {
                    pictureBox1.Image = Properties.Resources.delete;
                }
                else
                {
                    pictureBox1.Image = Properties.Resources.tick;
                }
            }
        }
 
        private void txbIPaddress_Leave(object sender, EventArgs e)
        {
            if (txbIPaddress.Text.Length > 0)
            {
                string pattern = @"\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b";
                if (!Regex.IsMatch(txbIPaddress.Text, pattern))
                {
                    pictureBox1.Image = Properties.Resources.delete;
                    MessageBox.Show("Wrong IPv4 address!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txbIPaddress.Select();
                    txbIPaddress.Text = "";
                }
                else
                {
                    pictureBox1.Image = Properties.Resources.tick;
                }
            }
 
        }
private void txbMask_TextChanged(object sender, EventArgs e)
        {
            if (txbMask.Text.Length == 0)
            {
                pictureBox2.Image = null;
            }
            else
            {
                string pattern = @"^(255|254|252|248|240|224|192|128|0+)\.(255|254|252|248|240|224|192|128|0+)\.(255|254|252|248|240|224|192|128|0+)\.(255|254|252|248|240|224|192|128|0+)$";
                if (!Regex.IsMatch(txbMask.Text, pattern))
                {
                    pictureBox2.Image = Properties.Resources.delete;
                }
                else
                {
                    pictureBox2.Image = Properties.Resources.tick;
                }
            }
        }
 
        private void txbMask_Leave(object sender, EventArgs e)
        {
            if (txbMask.Text.Length > 0)
            {
                string pattern = @"^(255|254|252|248|240|224|192|128|0+)\.(255|254|252|248|240|224|192|128|0+)\.(255|254|252|248|240|224|192|128|0+)\.(255|254|252|248|240|224|192|128|0+)$";
 
                if (!Regex.IsMatch(txbMask.Text, pattern))
                {
                    pictureBox2.Image = Properties.Resources.delete;
                    MessageBox.Show("Wrong IPv4 netmask!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txbMask.Select();
                    txbMask.Text = "";
                }
                else
                {
                    pictureBox2.Image = Properties.Resources.tick;
                }
            }
        }

And here’s the result if you enter wrong IP address:

And entering wrong network mask address:

After correcting the addresses: