ASP.Net Regular Expressions
The RegularExpressionValidator control is an often overlooked tool for validation. A lot of people look at the validation expression and are puzzled at how to proceed. Most settle on the RequiredFieldValidator and hand coded filtering in the submit action. I hope to compile here a list of common expressions needed for web form validation so that your job is easier.
Why Regular Expressions
Forming a regular expression (sometimes shortened to RegEx) is not the easiest task since it requires a lot of “what if?” thinking about potential user input. There is also a large barrier to entry for the common programmer since they must first learn all of the special characters and how each of them works, then how they all work together when data is processed. It can take a very long time for a programmer to become proficient at creating the perfect expression. I have had a fair bit of experience at this but by no means would I call myself an expert.
When creating web forms we have the option to make them as flexible or as rigid as we want. On the rigid side we can require that the name is entered separately in first and last name boxes or that the phone number is entered in area code, exchange and number boxes, etc. This makes data collection and reporting very easy after the fact. On the flexible side the name could be one box, as well as the phone number, etc. I have seen both methods in action and the resulting data sets.
Data from the rigid side is very organized and clean and poses few issues when running queries after the fact. Data from the flexible side on the other hand is messy and requires much processing and special considerations to use. You might suspect from this that I think the rigid method is the best but you would be wrong. I am a big proponent of the flexible solution because I know how it can be done right and the result is a much friendlier user entry experience. All data collection issues can be solved with a combination of regular expressions and clean-up algorithms.
The RegEx List
Email Address
^[a-zA-Z0-9\-\._]+@([a-zA-Z0-9\-_]+\.)+[a-zA-Z]{2,4}$
Phone Number (requires area code)
^\+?1?-?(\(\d{3}\)|\d{3})-?\d{3}-?\d{4}$
Phone Number (optional area code)
^\+?1?-?(\(\d{3}\)|\d{3}|)-?\d{3}-?\d{4}$
Curreny Amount (including dollars and cents)
^\$?\d+(\.\d{2})?$
Name (last, first)
^[a-zA-Z\-'\s]+,\s*[a-zA-Z\-'\.]+(\s+[a-zA-Z\-'\.]+)*$
Name (first, last)
^[a-zA-Z\-'\.]+(\s+[a-zA-Z\-'\.]+)+$
Postal Code
^[a-zA-Z][0-9][a-zA-Z]\s*[0-9][a-zA-Z][0-9]$
Zip Code
^\d{5}(\-?\d{4})?$
Clean-up Algorithms
These regular expressions allow for the user to enter most of the common syntax for a particular item which then allows you to write a clean-up function that changes the information into the format you want to store in the database. These functions would take the user input, check for the various cases (i.e. has area code brackets or not) and return it in the correct format. I recommend starting a module of these functions that each handle a different input type. After a short period of time you will find that you are able to throw together a rock solid flexible form in a few hours.
Feedback and expression requests are much appreciated. Let me know how these work out for you. Here are a few resources which I have found extremely useful: Regular Expressions in ASP.Net (MSDN) and the Regular Expression Tester.
Your site so helpful for us.
hello
i want regular expression for username that must take only one space and alphanumerics.other special symbols should not be there.
plz do reply me.ur explanation was gud.
but its not working
WordPress was stripping out my backslashes! I’ve fixed the problem now, sorry for the inconvenience.
Do you have examples of the clean-up functions that changes the information into the correct format for storing in the database? Can this be done “onBlur”? I am very new at ASP.Net and have used regular expressions in javascript but am not sure how to reformat data input in ASP.Net.
onblur is a JavaScript function and I recommend against using JavaScript for anything that is required to happen. There is no guarantee that the JavaScript code will run in a users browser.
Instead it is best to put the clean-up code in the submit function just before inserting it into the database. You can now be assured that the data will be formatted properly.
An example clean-up algorithm:
' handle phone variations If phone.Contains("(") Then ' extract digits ElseIf phone.Contains("-") Then ' extract digits ... End IfI want to date vaildation script, for format specify
Here is another one for phone number off of the one above
this one can take multiple type of formats
1231231234
123-123-1234
(123)123-1234
^\d{10}$|^\+?1?-?(\(\d{3}\)|\d{3})-?\d{3}-\d{4}$|^\d{3}-d{3}-d{4}$
i want a validator for yhe following expression
nnnnnnnnnnnnnn.nnn as a maximum
hello
i want to regular expression validation for the Austrilian Postal code
Thank you very much for the multiple format validation of zip code and phone number!
Nice reference page, thanks!
I hope Hardik has his answer by now, but for other people with the same question, the Australian postcode is one of the easiest regular expressions, as it is simply a 4 digit code. The lowest one I know of is 0800 and the highest is 9999.
The regexp would simply be “\d{4}”
ie; digits exactly of quantity 4.