Monday, July 31, 2006

C# .Net Regular Expression For Credit Card, SSN, Address, URL, Phone etc.

The solution example defines 16 regular expression validation functions and a helper method that address a common set of application input data types. The regular expression validation functions are:

IsValidUserName IsValidCCNumber
IsValidPassword IsValidSSN
IsValidName IsValidEmailAddress
IsValidStreetAddress IsValidURL
IsValidCity IsValidIPAddress
IsValidUSState IsValidAlphaText
IsValidZIPCode IsValidAlphaNumericText
IsValidUSPhoneNumber IsValidNumericText


// Decomposed method which actually creates the pattern object and determines the match.
// Used by all of the other functions.
static bool MatchString(string str, string regexstr)
{
str = str.Trim();
System.Text.RegularExpressions.Regex pattern = new System.Text.RegularExpressions.Regex(regexstr);


return pattern.IsMatch(str);
}


static bool IsValidUserName(string strUsername)
{
// Allows word characters [A-Za-z0-9_], single quote, dash and period
// must be at least two characters long and less then 128
string regExPattern = @"^[\w-'\.]{2,128}$";


// We also permit email address characters in user name. Set to false
// if you don't permit email addresses as usernames.
bool allowEmailUsernames = true;


if (allowEmailUsernames)
{
return (MatchString(strUsername, regExPattern) || IsValidEmailAddress(strUsername));
}
else
{
return MatchString(strUsername, regExPattern);
}
}


static bool IsValidPassword(string strPassword)
{
// Allows any type of character


// If complexity is enabled, the password must be longer
// and contain at least one uppercase, one lowercase,
// one numeric and one symbolic character. Set to false
// if your requirements differ.
bool passwordComplexity = true;


// These are some proposed minimum password lengths. If
// complexity is enabled (above), the stronger (longer)
// minimum password rule applies.
int minPasswordLen = 6;
int strongPasswordLen = 8;


if(passwordComplexity) {
string regExPattern = @"^.*(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[`~!@#\$%\^\&\*\(\)-_\=\+\[\{\]\}\\\|;:',<\.>/?]).*$";
return(strPassword.Length >= strongPasswordLen &&
MatchString(strPassword, regExPattern));
} else {
return(strPassword.Length >= minPasswordLen);
}
}


static bool IsValidName(string strName)
{
// Allows alphabetical chars, single quote, dash and space
// must be at least two characters long and caps out at 128 (database size)
string regExPattern = @"^[a-zA-Z-'\.\s]{2,128}$";
return MatchString(strName, regExPattern);
}


static bool IsValidStreetAddress(string strAddress)
{
// Since so many different types of address formats we're just going to swing the bat at
// this one for now and do a match against a series of digits (potentially containing
// punctuation), followed by a series of characters representing the street name and then
// potentially a type of street and unit number
string regExPattern = @"\d{1,3}.?\d{0,3}\s[a-zA-Z]{2,30}(\s[a-zA-Z]{2,15})?([#\.0-9a-zA-Z]*)?";
return MatchString(strAddress, regExPattern);
}


static bool IsValidCity(string strCity)
{
// Here we simply treat city names like people names and defer to our name validation function.
return IsValidName(strCity);
}


static bool IsValidUSState(string strState)
{
// Names of 50 US States
string[] stateNames= {"ALABAMA","ALASKA","ARIZONA","ARKANSAS","CALIFORNIA","COLORADO","CONNECTICUT","DELAWARE","FLORIDA","GEORGIA","HAWAII","IDAHO","ILLINOIS","INDIANA","IOWA","KANSAS","KENTUCKY","LOUISIANA","MAINE","MARYLAND","MASSACHUSETTS","MICHIGAN","MINNESOTA","MISSISSIPPI","MISSOURI","MONTANA","NEBRASKA","NEVADA","NEW HAMPSHIRE","NEW JERSEY","NEW MEXICO","NEW YORK","NORTH CAROLINA","NORTH DAKOTA","OHIO","OKLAHOMA","OREGON","PENNSYLVANIA","RHODE ISLAND","SOUTH CAROLINA","SOUTHDAKOTA","TENNESSEE","TEXAS","UTAH","VERMONT","VIRGINIA","WASHINGTON","WEST VIRGINIA","WISCONSIN","WYOMING"};
// Postal codes of 50 US States
string[] stateCodes = {"AL","AK","AZ","AR","CA","CO","CT","DE","DC","FL","GA","HI","ID","IL","IN","IA","KS","KY","LA","ME","MD","MA","MI","MN","MS","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","OH","OK","OR","PA","RI","SC","SD","TN","TX","UT","VT","VA","WA","WV","WI","WY"};




// This one is somewhat easier because we have a finite set of values to check against.
// We simply uppercase our value anc check against our list.
strState = strState.ToUpper();


ArrayList stateCodesArray = new ArrayList(stateCodes);
ArrayList stateNamesArray = new ArrayList(stateNames);


return (stateCodesArray.Contains(strState) || stateNamesArray.Contains(strState));
}


static bool IsValidZIPCode(string strZIP)
{
// Allows 5 digit, 5+4 digit and 9 digit zip codes
// must be at least two characters long and caps out at 128 (database size)
string regExPattern = @"^(\d{5}-\d{4}|\d{5}|\d{9})$";
return MatchString(strZIP, regExPattern);
}


static bool IsValidUSPhoneNumber(string strPhone)
{
// Allows phone number of the format: NPA = [2-9][0-8][0-9] Nxx = [2-9][0-9][0-9] Station = [0-9][0-9][0-9][0-9]
string regExPattern = @"^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$";
return MatchString(strPhone, regExPattern);
}


static bool IsValidCCNumber(string strCCNumber)
{
// This expression is basically looking for series of numbers confirming to the standards
// for Visa, MC, Discover and American Express with optional dashes between groups of numbers
string regExPattern = @"^((4\d{3})|(5[1-5]\d{2})|(6011))-?\d{4}-?\d{4}-?\d{4}|3[4,7][\d\s-]{15}$";
return MatchString(strCCNumber, regExPattern);
}

static bool IsValidSSN(string strSSN)
{
// Allows SSN's of the format 123-456-7890. Accepts hyphen delimited SSN’s or plain numeric values.
string regExPattern = @"^\d{3}[-]?\d{2}[-]?\d{4}$";
return MatchString(strSSN, regExPattern);
}


static bool IsValidEmailAddress(string strEmail)
{
// Allows common email address that can start with a alphanumeric char and contain word, dash and period characters
// followed by a domain name meeting the same criteria followed by a alpha suffix between 2 and 9 character lone
string regExPattern = @"^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$";
return MatchString(strEmail, regExPattern);
}


static bool IsValidURL(string strURL)
{
// Allows HTTP and FTP URL's, domain name must start with alphanumeric and can contain a port number
// followed by a path containing a standard path character and ending in common file suffixies found in URL's
// and accounting for potential CGI GET data
string regExPattern = @"^^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_=]*)?$";
return MatchString(strURL, regExPattern);
}


static bool IsValidIPAddress(string strIP)
{
// Allows four octets of numbers that contain values between 4 numbers in the IP address to 0-255 and are separated by periods
string regExPattern = @"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$";
return MatchString(strIP, regExPattern);
}


static bool IsValidAlphaText(string strAlpha)
{
// Allows one or more alphabetical characters. This is a more generic validation function.
string regExPattern = @"^[A-Za-z]+$";
return MatchString(strAlpha, regExPattern);
}


static bool IsValidAlphaNumericText(string strAlphaNum)
{
// Allows one or more alphabetical and/or numeric characters. This is a more generic validation function.
string regExPattern = @"^[A-Za-z0-9]+$";
return MatchString(strAlphaNum, regExPattern);
}


static bool IsValidNumericText(string strNumeric)
{
// Allows one or more positive or negative, integer or decimal numbers. This is a more generic validation function.
string regExPattern = @"/[+-]?\d+(\.\d+)?$";
return MatchString(strNumeric, regExPattern);
}

Wednesday, July 26, 2006

My sysadmin toolbox

I've been a system administrator since 1988, working mainly with Solaris and one or two versions of BSD. Here are some of the things I use all the time, including a number of scripts I've written myself to leverage already useful *nix tools; they're not flashy, but they save me a ton of keystrokes.

read more | digg story

Tuesday, July 25, 2006

25 of the best extensions for Firefox - Have It Your Way!

The following is a list of the best 25 extensions for Firefox. They have been classified into three board categories: those that add additional enhancements to the browser and improve a user's experience, those that add additional enhancements to certain web sites, and ...

read more | digg story

Monday, July 24, 2006

Mouse wheel programming in JavaScript

Programming mouse wheel event handling in JavaScript. Works on IE, Firefox and Opera 9. Might be useful to web application developers.

read more | digg story

Saturday, July 22, 2006

Over 100 CSS demos

"A lot of these are experiments without explanation but a lot of people find them interesting, so I will list them here until I get around to documenting them fully. Use them at your own risk and I hope you find them useful."

read more | digg story

Friday, July 21, 2006

Dual-booting Windows and Linux the easy way (Linux.com videos)

An excellent step-by-step videos-based guide. There is no longer a reason to have any machine in the house/office which is Windows-only.

read more | digg story

Tuesday, July 18, 2006

Windows Freeware Applications

There are dozens of well known freeware applications out there. From web browsers to word processors to anti-viruses, there is freeware everywhere. Take a look at these 10 great Windows freeware applications you have probably either never heard of or never fully looked into.

Sunday, July 16, 2006

TOP 69 CSS Menus with Tutorials and Downloads

The ultimate CSS Menu resource with screenshots, tutorials and how-tos.

read more | digg story

Saturday, July 15, 2006

Library of free database models

The library includes hundreds of sample database schemas for tons of topics � ranging from �Airline Reservations� to �Organizations and People� to �Car Servicing� to �Pizza Delivery�.

read more | digg story

Wednesday, July 12, 2006

DHTML Goodies - Drag Droppable Tree Control

The title says it all...aint it!




A Closer Look at Damn Small Linux ( DSL )

In this article you will learn how to turn a blank CD and an inexpensive USB keydrive into a powerful, portable, take-along operating system complete with modern applications like Firefox, a Web server, and multimedia tools. All this can be done using free Open Source Linux software.

Sunday, July 09, 2006

Setting Up Your Own DNS

This article will describe Internet domain names and the structure of the DNS, followed by a brief tutorial about creating and maintaining the DNS information.

read more | digg story

Yahoo Mail Search Just Got Better


Just logged in yahoo mail and saw a little nice cloud box near the search box saying that


Yahoo Mail Search Just Got Better


If you have much more time to spare to read more about this update go ahead and click here (which has few screen-shots) and ignore my article. Below I tried to put a quick summary of the same.

Digging more here is a quick info about the updates..
Yahoo mail now provides 3 different views viz..

Message View: Shows short snippets of the mail content by default and it shows the snippet containing the terms provided you searched for those terms.

Photos View: I really liked this feature which shows all of the pictures in your inbox and easy way to search them. I myself was surprised to see that my inbox had 903images :O ....

Attachment View: This shows the attachments. Looks very similar to the Photos views.

Also there is an easy way to filter the results and the views on the left side column.

Keeping the Yahoo Mail Web 2.0 in Beta and working on the classic version is weird and does it mean the new version going to be in Beta for long time from now...!
I checked my other yahoo account which I tweaked using the yahoo.uk domain to use the web 2.0 version and couldnt find any search updates.

Yahoo should hurry up and atleast provide Beta to all instead of making people to wait and wait and start using Gmail.

Saturday, July 08, 2006

the query processor could not produce a query plan from the optimizer because a query cannot update a text ntext or image column and a clustering key

Heres is the solution if see the error something like:

the query processor could not produce a query plan from the optimizer because a query cannot update a text ntext or image column and a clustering key at the same time.



In your update sql statement dont set the primary key and index/clustered key values
and also i guess the ntext or image fields.... and that shud fix this silly problem which I know is annoying.

Thursday, July 06, 2006

Getting Your Site Indexed Before You Launch

The key to getting your site indexed in the big three is getting links pointing to it from sites that are already indexed. When the search bots crawl those sites they will inevitably find the link to your site and your site will be added to their index. Follow these five steps a month before you launch and you'll be a step ahead of the game.

Monday, July 03, 2006

go forth and API

Practical advice on how best to use APIs

read more | digg story

Sunday, July 02, 2006

Resume Tips

Some must tips which are must to be followed if you
are looking for a job.