JavaScript
  Home arrow JavaScript arrow Validators: Into the Deep
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Validators: Into the Deep
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 9
    2005-09-13

    Table of Contents:
  • Validators: Into the Deep
  • Email, Mask, Date, and Length Validators
  • Constants and Variables
  • Registration Form: Putting it All Together

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Validators: Into the Deep


    (Page 1 of 4 )

    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.

    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.

    More JavaScript Articles
    More By A.P.Rajshekhar


       · HiThis is second part of my validator framework series. The framework works with...
     

    JAVASCRIPT ARTICLES

    - Book Review: Learning the Yahoo! User Interf...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget
    - Ajax Hack for Entering Information Without R...
    - EXT JS 2.1 Overview
    - Using the Style Object for Zebra Tables with...
    - Binary Searching
    - An Improved Approach to Building Zebra Tables
    - Assigning Background Colors Dynamically to Z...
    - Building Zebra Tables with CSS and JavaScript
    - JavaScript: Array Objects
    - A Closer Look at Smart Markers with Yahoo! M...
    - Using Polylines and Smart Markers with Yahoo...







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway