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 with .net – U.S. Social Security Numbers


Regular Expressions with .net – U.S. Social Security Numbers

In the United States, a Social Security number (abbreviated as SSN) is a nine-digit number issued to U.S. citizens, permanent residents, and temporary (working) residents. U.S. social security numbers are three sets of digits separated by hyphens; the first set contains three digits, the second set contains two digits, and the third set contains four digits. Its primary purpose is to track individuals for taxation purposes. In recent years the SSN has become a de facto national identification number. A properly formatted US social security number. first three digits must be 001 – 772.

Regular Expression Pattern

^((?!000)([0-6]\d{2}|[0-7]{2}[0-2]))-((?!00)\d{2})-((?!0000)\d{4})$

A description of the regular expression:

[1]: A numbered capture group. [(?!000)([0-6]\d{2}|[0-7]{2}[0-2])]
(?!000)([0-6]\d{2}|[0-7]{2}[0-2])
Match if suffix is absent. [000]
[2]: A numbered capture group. [[0-6]\d{2}|[0-7]{2}[0-2]]
Select from 2 alternatives
[0-6]\d{2}
Any character in this class: [0-6]
Any digit, exactly 2 repetitions
[0-7]{2}[0-2]
Any character in this class: [0-7], exactly 2 repetitions
Any character in this class: [0-2]
-
[3]: A numbered capture group. [(?!00)\d{2}]
(?!00)\d{2}
Match if suffix is absent. [00]
Any digit, exactly 2 repetitions
-
[4]: A numbered capture group. [(?!0000)\d{4}]
(?!0000)\d{4}
Match if suffix is absent. [0000]
Any digit, exactly 4 repetitions

How It Works

This regular expression will check for valid U.S. Social Security Numbers. Here we are going to search three sets of digits separated by hyphens; the first set contains three digits, the second set contains two digits, and the third set contains four digits.

First first group will check wheather all digit are not zero. Then we are performing search between two alternatives, eighter in-between 001 to 699 or in-between 001 to 772.

Second group will check wheather two digit are not zero and any range in-between 01 to 99

Second group will check wheather four digit are not zero and any range in-between 0001 to 9999

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>U.S. Social Security Numbers</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<span>Valid Format: 123-45-6789</span><br />
<asp:TextBox id="txtInput" runat="server"></asp:TextBox><br />
<asp:RegularExpressionValidator Id="vldRejex" RunAt="server" ControlToValidate="txtInput" ErrorMessage="Please enter a valid Social Security Numbers" ValidationExpression="^((?!000)([0-6]\d{2}|[0-7]{2}[0-2]))-((?!00)\d{2})-((?!0000)\d{4})$">
</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 Social Security Numbers";
lblMsg.ForeColor = Color.Red;
}
}
private void checkRejex(string strFindin)
{
Regex myRegex = new Regex("^((?!000)([0-6]\\d{2}|[0-7]{2}[0-2]))-((?!00)\\d{2})-((?!0000)\\d{4})$");
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 Social Security Numbers"
lblMsg.ForeColor = Color.Red
End If
End Sub
Private Sub checkRejex(ByVal strFindin As String)
Dim myRegex As New Regex("^((?!000)([0-6]\d{2}|[0-7]{2}[0-2]))-((?!00)\d{2})-((?!0000)\d{4})$")
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

Downloads:

Chetan love blogging. He regularly blogs at http://www.tipsntracks.com. You can connect with Chetan on Twitter, Facebook and Google Plus...

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 with .net – U.S. Social Security Numbers [...]

Pingback by Text and Data Manipulation with Regular Expressions in .NET Development | Tips n Tracks on May 29, 2009 @ 5:07 am

Leave a comment

(required)

(required)

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