Thursday, August 18, 2011

Regular expression for AANNNNNNNA and AAANNNNNNN format with exact max length 10.

Place two text boxes and two buttons in aspx page and write the code in the code behind.cs file

Code Behind File:

using System.Text.RegularExpression;

bool sDigits=false;

protected void Button1_click()
{

//AANNNNNNNA Format

sDigits = Regex.IsMatch(TextBoxName.Text.Trim().ToUpper(), @"^[A-Z]{2}[0-9]{7}[A-Z]{1}$");
if(sDigits== true)
Response.Write("Format is valid");
else
Response.Write("Format is invalid");
}

protected void Button2_click()
{

//AAANNNNNNN Format

sDigits = Regex.IsMatch(TextBoxName.Text.Trim().ToUpper(), @"^[A-Z]{3}[0-9]{7}$");
if(sDigits== true)
Response.Write("Format is valid");
else
Response.Write("Format is invalid");
}