Home arrow JavaScript arrow Validators: Into the Deep
JAVASCRIPT

Validators: Into the Deep


In the first part, we saw a glimpse of what the Validator framework can do. But that is just the tip of the iceberg. The driving force behind creation of the Validator framework is reusability. This facet of the framework is but one of the many aspects that can not only reduce the web GUI development time but also enforce standards across all interfaces.

Author Info:
By: A.P.Rajshekhar
Rating: 5 stars5 stars5 stars5 stars5 stars / 14
September 13, 2005
TABLE OF CONTENTS:
  1. · Validators: Into the Deep
  2. · Email, Mask, Date, and Length Validators
  3. · Constants and Variables
  4. · Registration Form: Putting it All Together

print this article
SEARCH DEVARTICLES

Validators: Into the Deep
(Page 1 of 4 )

In this part I shall take you into the depth of Validator framework so that by the end, resolving all the major validation cases will be "easy like the wind." The first section will deal with the types of Validators, and in the second section all of these Validators will be put to use in validating inputs of the first point in user management -- the "Registration page."

So let’s start by examining the various types of validations provided by the framework.

Types of Validators

The Validators, or the Standard rules, which is the other  term by which they are also known, come in varieties. They are:

  1. Required Validator
  2. Data Type Validator
  3. Range Validator
  4. Email Validator
  5. Mask Validator
  6. Date Validator
  7. Length Validator

Let's take a look at them one by one.

Required Validator

This rule is used to validate mandatory fields. It is the commonly used validation. The fields where this rule is applicable are user id, password, address, and so on. For example, if the name of the field is “Password” then the required rule would be:
 
 <field property="name" depends="required">
         <arg0 key="registration.Name"/>
     </field>

Data Type Validator

There are occasions when the data should be of a specific type. The best example is the field specifying age. It must be numeric. So the data type Validator can be applied. This rule can validate six types of data: 

  1. byte
  2. short
  3. integer
  4. float
  5. long
  6. double


These correspond to the basic Java data types. What the rule checks is whether the input data could be converted to the data type provided as a part of the rule. The example of the age field would provide a better illustration.

 <field property="age" depends="byte">
        <arg0 key="registration.age"/>
    </field>

Age can (almost) never cross 100. So the data entered must be less than or equal to one hundred. Hence the byte rule will fail. To validate the other data types, replace byte with the corresponding above mentioned type as called for by the value.

Range Validator

The range Validator does what the name suggests. If the rule is to make sure that the data entered is between specific ranges, then Range Validators are the rules to be applied. Before version 1.2, the Validator framework contained only the “range”  rule. Today, to use the range Validator, one has to specify one of the following Validators/rules:

  1. intRange
  2. longRange
  3. floatRange
  4. doubleRange

These Validators correspond directly with the data type. That is, intRange is used for checking whether the input is an integer in the valid range. The same is true with the other range Validators. To cite a quick example, if the data to be validated is age (again), then the rule would look like this:

 <field property="age" depends="required,integer,intRange">
         <arg0 key="registration.age"/>

<arg1 name="intRange" key="${var:min}"  resource="false"/>

        <arg2 name="intRange" key="${var:max}" resource="false"/>
        <var><var-name>min</var-name><var-value>12</var-value></var>
        <var><var-name>max</var-name><var-value>100</var-value></var>
      </field>

When compared with other Validators, the range Validator is different. It uses elements/tags <arg1> and <arg2> to specify the minimum and maximum values for the range. The value provided as the key is used in <var> to provide the value for valid minimum and maximum values. Here valid values are between 12 and 100. Hence the <var> tags are used to specify the value.  I will explain more about <var> later when I discuss variable and constants.


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- Project Nashorn to Make Java, JavaScript Wor...
- JavaScript Virus Attacks Tumblr Blogs
- Google Releases Stable Dart Version, JavaScr...
- Khan Academy Unveils New JavaScript Learning...
- Accessing Nitro? There`s an App for That
- JQuery 2.0 Leaving Older IE Versions Behind
- Fastest JavaScript Engine Might Surprise You
- Microsoft Adjusting Chakra for IE 10
- Brendan Eich: We Don`t Need Google Native Cl...
- An Overview of JavaScript Statements
- An Overview of JavaScript Operators
- Overview of JavaScript Variables
- More of the Top jQuery Social Plugins
- The Top jQuery Social Plugins
- More of the Top jQuery Slider Plugins

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials