Tips n Tracks

  • Increase font size
  • Default font size
  • Decrease font size
  • default color
  • black color

Reference

Sample image

Microsoft .NET Framework Get Details.

Sample image

Microsoft .NET Framework Get Details.

Reference

Sample image Microsoft .NET Framework Get Details.
Sample image

Microsoft .NET Framework Get Details.


Regular Expressions IP Address Validation with .net


Regular Expressions IP Address Validation with .net

An Internet Protocol (IP) address is a numerical identification (logical address) that is assigned to devices participating in a computer network utilizing the Internet Protocol for communication between its nodes.

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)$

A description of the regular expression:

[1]: A numbered capture group. [([01]?\d\d?|2[0-4]\d|25[0-5])\.], exactly 3 repetitions
([01]?\d\d?|2[0-4]\d|25[0-5])\.
[2]: A numbered capture group. [[01]?\d\d?|2[0-4]\d|25[0-5]]
Select from 3 alternatives
[01]?\d\d?
Any character in this class: [01], zero or one repetitions
Any digit
Any digit, zero or one repetitions
2[0-4]\d
2
Any character in this class: [0-4]
Any digit
25[0-5]
25
Any character in this class: [0-5]
Literal .
[3]: A numbered capture group. [[01]?\d\d?|25[0-5]|2[0-4]\d]
Select from 3 alternatives
[01]?\d\d?
Any character in this class: [01], zero or one repetitions
Any digit
Any digit, zero or one repetitions
25[0-5]
25
Any character in this class: [0-5]
2[0-4]\d
2
Any character in this class: [0-4]
Any digit

Sucessful Matches

172.0.0.1
0.1.2.0
16.5.1.3
239.2.2.2
172.0.0.250
123.123.234.123

How It Works

This regular expression will check for valid Internet Protocol (IP) address. Here we are going to search four decimal numbers groups, each separated by a dot. each set contains zero to three digits.

Here regular expression check for two group which on concatenation generate a valid IP address. First group select from 3 alternatives which will find decimal number range from 0 to 255 ending with .(dot or period), exactly 3 repetitions and Second group select from 3 alternatives which will find decimal number range from 0 to 255.

ASP.NET

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Regular Expressions with .net – Validate IP Address</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<span>Valid Format: 172.0.0.1</span><br />
<asp:TextBox id="txtInput" runat="server"></asp:TextBox><br />
<asp:RegularExpressionValidator Id="vldRejex" RunAt="server"
ControlToValidate="txtInput"
ErrorMessage="Please enter a valid IP Address"
ValidationExpression="^(([01]?\d\d?|2[0-4]\d|25[0-5])\.){3}([01]?\d\d?|25[0-5]|2[0-4]\d)$">
</asp:RegularExpressionValidator><br />
<asp:Button Id="btnSubmit" RunAt="server" CausesValidation="True" Text="Submit"></asp:Button>
</div>
</form>
</body>
</html>

C#.NET

using System;
using System.IO;
using System.Text.RegularExpressions;
namespace SSN_CSharp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnValidate_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtInput.Text)) {
checkRejex(txtInput.Text);
}
else {
lblMsg.Text = "Please enter a valid IP Address";
lblMsg.ForeColor = Color.Red;
}
}
private void checkRejex(string strFindin)
{
Regex myRegex = 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))
{
lblMsg.Text = "Valid Input";
lblMsg.ForeColor = Color.Green;
}
else
{
lblMsg.Text = "Please enter a valid Social Security Numbers";
lblMsg.ForeColor = Color.Red;
}
}
}
}

VB.NET

Imports System
Imports System.IO
Imports System.Text.RegularExpressions
Public Class Form1
 
Private Sub btnValidate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnValidate.Click
If txtInput.Text <> "" Then
checkRejex(txtInput.Text)
Else
lblMsg.Text = "Please enter a valid IP Address"
lblMsg.ForeColor = Color.Red
End If
End Sub
Private Sub checkRejex(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
lblMsg.Text = "Valid Input"
lblMsg.ForeColor = Color.Green
Else
lblMsg.Text = "Please enter a valid Social Security Numbers"
lblMsg.ForeColor = Color.Red
End If
End Sub
End Class

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

[...] Regular Expressions IP Address Validation with .net [...]

Pingback by Text and Data Manipulation with Regular Expressions in .NET Development | Tips n Tracks on November 21, 2009 @ 11:23 am

Leave a comment

(required)

(required)

*
To prove that you're not a bot, enter this code
Anti-Spam Image