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