Tuesday 14 February 2012

Regular Expressions IP Address Validation with .net

 Regular Expressions IP Address Validation with .net

IP Address Classes:

IP addresses are categories into five classes: Class A, Class B, Class C, Class D and Class E. Each class allows for a range of valid IP addresses. whereas classes A, B and C are used to communication.

Class A Internet Protocol (IP) address ranges include 1.0.0.0 to 126.255.255.255. This class supports 16 million hosts on each of the 127 networks. network 0.0.0.0 is reserved for use as the default route and the network 127.0.0.0 is reserved for the “loopback” function.

Class B Internet Protocol (IP) address ranges include 128.1.0.0 to 191.255.255.255. This class supports 65,000 hosts on each of the 16,000 networks.

Class D Internet Protocol (IP) address ranges include 224.0.0.0 to 239.255.255.255. This class is used to support multicasting.

Class E Internet Protocol (IP) address ranges include 240.0.0.0 to 254.255.255.254. This class is used for experimentation. Class E networks have never been documented or utilized in a standard way.


IP addresses are also commonly used to divide networks into smaller ones. This concept is called subnetting. Subnet addresses include 255.0.0.0 to 255.255.255.255


Dotted-Decimal Notation

To make Internet addresses easier for people to read and write, IP addresses are often expressed as four decimal numbers, each separated by a dot. This format is called “dotted-decimal notation.” Dotted-decimal notation divides the 32-bit Internet address into four 8- bit fields and specifies the value of each field independently as a decimal number with the fields separated by dots.

Regular Expression Pattern

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


Imports System
Imports System.IO
Imports System.Text.RegularExpressions
Public Class Form1
    Private Sub CheckIPNumber_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckIPNumber.Click
        If txtInput.Text <> "" Then
            CheckValidate(txtInput.Text)
        Else
            LblInfo.Text = "Please enter a valid IP Address"
            LblInfo.ForeColor = Color.Red
        End If
    End Sub
    Private Sub CheckValidate(ByVal strFindin As String)
        Dim myRegex As New Regex("^(([01]?\d\d?|2[0-4]\d|25[0-5])\.){3}([01]?\d\d?|25[0-5]|2[0-4]\d)$")
        If myRegex.IsMatch(strFindin) Then
            LblInfo.Text = "Valid Input"
            LblInfo.ForeColor = Color.Green
        Else
            LblInfo.Text = "Please enter a valid Ip Number"
            LblInfo.ForeColor = Color.Red
        End If
    End Sub
End Class



No comments:

Post a Comment