Click here to Skip to main content
13,291,510 members (72,267 online)
Click here to Skip to main content
Add your own
alternative version


23 bookmarked
Posted 6 Mar 2005

Validating Data Using Regular Expression / XML

, 29 Apr 2005
Rate this:
Please Sign up or sign in to vote.
An article on how to use regular expressions for data validation (using XML).


Regular expressions are the best and the easiest way to do data validation. If you want to check for a valid date, then programming for it will be very complicated. A simple solution is to create a regular expression for what you need, and compare the input with the expression itself. Regular expressions are very powerful as they can reduce the size of the code and are also easy to understand.

The regular expression validator attached with this article allows users to create their own rules and then check for its validity.

If you see the source files, you will find the use of Datatable, XML and RegEx for achieving this task. This combination will work for large databases. But if you want to simplify the code, the Datatable can be removed and all the working can be directly performed through XML and RegEX.

The validation rules can be made for three basic data types: Date, Number and String. The rules can be used efficiently to make sure that the data is entered in correct format.

The code can be used in any application for comparing records so that the data entered is what we require. The use of XML makes it easier for storing the validation rules.

Using the code

Classes have been created for each data type to make it easier to handle. The user does not have to bother about the class. This classes are used for generating the regular expression, and in turn the XML for the rule selected by the user. The user just has to pass the XML string and the records get validated.

Data types


    You can specify the characters allowed in the string i.e., upper/lower case, special characters and whether to include space or not. Valid/invalid characters can be specified. The valid character means, it is acceptable and invalid means not allowed.


    It is possible to specify numbers in the rule including the maximum and minimum value for it. Currency symbols can also be checked by specifying the symbol. The position of the currency symbol can also be specified i.e., prefix or suffix.

  • DATE

    The date format can also be specified in the order of day, month and year. You can also specify the 1 or 01 pattern for day and month. The range can also be specified. The date has to be between the two dates that are specified in the range.

  • NONE

    Everything will be allowed, ignoring the invalid characters (if specified).

All the comparison is performed using regular expressions i.e., regular expressions are generated for the rules.

How to use the application

Follow these steps:

  • Select the data type for which you want to create the rule.
  • If String, then specify the characters that you want to allow (all characters, all numbers or all characters & numbers). Whether you want to allow space or not, can also be specified. Specify any valid / invalid character (optional).
  • If Number, specify the currency symbol, its position (prefix/suffix) and the decimal values allowed.
  • Press OK to generate the rule (i.e., the regular expression).
  • Enter the value to the textbox provided at the bottom, and press CHECK.
  • If the value entered matches the regular expression, relevant message will be shown at the bottom of the application. Also, if there are any errors, then these errors will be notified at the bottom of the application.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Vasant Raj
Web Developer
India India
Software engineer and currently working in SQL Server 2005 Reporting Services. I have done M.C.A. from M.S. University, Baroda.

You may also be interested in...


Comments and Discussions

Generalcan't see regular expression Pin
halibut876320-Jul-07 8:02
memberhalibut876320-Jul-07 8:02 
GeneralRegular Expression Pin
Dewang Ajmera.8-Jan-07 22:08
memberDewang Ajmera.8-Jan-07 22:08 
GeneralWhere in the Regular expression Pin
satesh19-Jul-06 21:32
membersatesh19-Jul-06 21:32 
GeneralRe: Where in the Regular expression Pin
Vasant Raj25-Jul-06 3:37
memberVasant Raj25-Jul-06 3:37 
QuestionWhere is the regular expression? Pin
korell13-Jul-05 11:31
memberkorell13-Jul-05 11:31 
AnswerRe: Where is the regular expression? Pin
Vasant Raj13-Jul-05 18:05
memberVasant Raj13-Jul-05 18:05 
GeneralRegular Expression Pin
Mehul__Patel6-Mar-05 22:15
memberMehul__Patel6-Mar-05 22:15 
GeneralRe: Regular Expression Pin
Vasant Raj6-Mar-05 22:57
memberVasant Raj6-Mar-05 22:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171207.1 | Last Updated 30 Apr 2005
Article Copyright 2005 by Vasant Raj
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid