Click here to Skip to main content
Click here to Skip to main content
Go to top

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).

Introduction

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

  • STRING

    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.

  • NUMBER

    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.

License

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

Share

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.

Comments and Discussions

 
Generalcan't see regular expression Pinmemberhalibut876320-Jul-07 7:02 
GeneralRegular Expression PinmemberDewang Ajmera.8-Jan-07 21:08 
GeneralWhere in the Regular expression Pinmembersatesh19-Jul-06 20:32 
GeneralRe: Where in the Regular expression PinmemberVasant Raj25-Jul-06 2:37 
QuestionWhere is the regular expression? Pinmemberkorell13-Jul-05 10:31 
AnswerRe: Where is the regular expression? PinmemberVasant Raj13-Jul-05 17:05 
GeneralRegular Expression PinmemberMehul__Patel6-Mar-05 21:15 
GeneralRe: Regular Expression PinmemberVasant Raj6-Mar-05 21:57 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 30 Apr 2005
Article Copyright 2005 by Vasant Raj
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid