Browse DevX
Sign up for e-mail newsletters from DevX


Java and XML: Learn a Better Approach To Data Validation : Page 4

Forget client-side script, hard-coded server-side data validation rules, and database retrieval. There's a better way to validate user input data using a combination of Java and XML.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Sample Form Definition
A sample forms definition file is shown below (the DTD specification has been omitted):

       <form key="user-info">
           <text-field key="first-name">
               <label>First Name</label>
           <text-field key="last-name" required="true">
               <label>Last Name</label>
           <integer-field key="age" required="true">
           <date-field key="birthday">
As you can see, the form has an attribute "key," as does each field. These values provide the unique identifier with which to reference forms and fields. Here are detailed explanations of the field properties specified:

  • label
The label is used by the engine when generating error messages and can be used to specify the actual label text when rendering the form to submit.

  • required
The required attribute specifies that some input must be supplied for the field to be considered valid.

  • default
The default attribute specifies the text that the field should default to.

  • max-length
Applies to text fields. In the example, it is used to specify the maximum length in characters for the First Name and Last Name fields.

  • min-inclusive
Applies to integer or decimal fields. Specifies the minimum acceptable value for the Age field in the example.

  • max-inclusive
Applies to integer or decimal fields. Specifies the maximum acceptable value for the Age field in the example.

Inter-Field Validation Rules
It is common for applications to need to apply validation rules to a certain field depending on the result of validation on another field. For example, an application might validate a ZIP code field only if a user specifies that she is inputting a U.S. address. The DTD for the meta-language we described above can be augmented to allow inter-field rules such as:

      <form key="addr-info">
         <text-field key="country" required="true">
         <text-field key="zip-code" 
            <label>ZIP Code</label>
            <rule name="zip-code-rule" 
               <eval opd1="country.input" 
                   opr="equals" opd2="United States"/>
In the example above, the "required" attribute of the "zip-code" field is obtained at validation time by evaluating rule "zip-code-rule," which checks whether the input value of the "country" field equals "United States."

The Balanced Approach
Data validation must be flexible, scalable, and portable; typical solutions are deficient in at least one of these key areas. The Java/XML-based data validation engine presented in this article addresses each of these concerns and yields the following benefits:

  • XML meta-language handles typical data validation logic and can be easily extended.
  • Easily and seamlessly integrated into any application that needs to validate data input. This is especially valuable for applications that need to support multi-channel clients (browsers, mobile devices, voice, and Web services) from a common codebase.
  • XML-based repository makes modifying validation rules and criteria easy and intuitive.
  • Localized validation rules and error messages simplify internationalization.
  • XML-based repository is lightweight and efficient.

Jing Xue is a Senior Software Engineer at Cysive, Inc. based in Reston, VA. Jing has worked with Java and XML since joining Cysive in 1999 and is currently engaged in development of Cymbio, Cysive's J2EE-based multi-channel product.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date