JavaScript
  Home arrow JavaScript arrow Page 3 - Validators: Concluding Remarks
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: Concluding Remarks
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 9
    2005-09-28

    Table of Contents:
  • Validators: Concluding Remarks
  • Creating Custom Validators
  • Define the validation logic
  • Create an entry in the Validator-rules.xml

  • 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: Concluding Remarks - Define the validation logic


    (Page 3 of 4 )

    Second, you must define the validation logic. The validation is contained within a method. This method is static. It can be give any name but the  parameters passed must be the references of the following:

    Object:

    The form bean is passed an object. The form bean contains the value of the field to be validated.

    ValidatorAction:

    It contains the information to dynamically instantiate and run the validation method. It represents the rule. It also represents the XML validator element that will be entered in validator-rules.xml in the third step.

    Field:

    It contains the list of rules, the associated form property, message arguments, messages, variables used in performing validations and generated error messages. In short, it represents the field element that is being used in validation.

    ActionErrors:

    When the validation fails, ActionError will have to be added to the ActionErrors.

    Apart from these, the fifth parameter is HttpServletRequest. Now let's see how to put the validation logic into code.

    public class PhoneRules
     {
       public static boolean validatePhone(
                                 Object bean,
                                 ValidatorAction va,
                                 Field field,
                                 ActionErrors errors,
                                 HttpServletRequest request)
        {
          :
          :
      }
    }

    Next, get the value entered in the Phone No field using the getValueAsString() method of BeanUtils class. It takes form bean and field name as arguments. The field name could be obtained from the Field parameter.

    public class PhoneRules
     {
       public static boolean validatePhone(
                                 Object bean,
                                 ValidatorAction va,
                                 Field field,
                                 ActionErrors errors,
                                 HttpServletRequest request)
        {
          //get the value entered in the field
          String phone = ValidatorUtil.
                       getValueAsString(bean,
                       field.getProperty());
                       :
                       :
     


      }
    }

     Now the actual logic comes into the picture.

     public class PhoneRules
     {
       public static boolean validatePhone(
                                 Object bean,
                                 ValidatorAction va,
                                 Field field,
                                 ActionErrors errors,
                                 HttpServletRequest request)
        {
          //get the value entered in the field
          String phone = ValidatorUtil.
                         getValueAsString(bean,
                         field.getProperty());
          
                         //Check to see if the value is a valid
                 phone
                  char [] chars = phone.toCharArray();
                  int numberCount=0;
            
           for (int index=0; index < chars.length; index++)
               {
                char c = chars[index];
                if (Character.isDigit(c))
                   {
                                numberCount++;
                    }
                    else if (Character.isWhitespace(c))
                                {
                                       //White space okay
                                }
                 else if (c==’+’||c == '(' || c ==')' || c
    =='-')
                          {
                            if(c==’+’&&numberCount=0)
                                ;//do nothing
                            else
                              return false;
                                        // ()- okay too
                          }    
                          else
                          {
                           return false;
                          } 
                    }
             }
    }

     The phone number can be of the form +91 (0755) 23401551 or +91 0755-23401551 (according to the Indian telephone number pattern). It also counts the number of digits. The maximum allowed number of digits is 14. If the number of digits is more than that, the validation fails. To generate an error when the validation fails, the error has to be added to the action errors. It can be accomplished like this:

    public class PhoneRules
     {
       public static boolean validatePhone(
                                 Object bean,
                                 ValidatorAction va,
                                 Field field,
                                 ActionErrors errors,
                                 HttpServletRequest request)
        {
          //get the value entered in the field
          String phone = ValidatorUtil.
                         getValueAsString(bean,
                         field.getProperty());
          
                        //Check to see if the value is a valid phone
                  char [] chars = phone.toCharArray();
                  int numberCount=0;
            
           for (int index=0; index < chars.length; index++)
               {
               char c = chars[index];
               if (Character.isDigit(c))
                  {
                                  numberCount++;
                   }
                   else if (Character.isWhitespace(c))
                              {
                                      //White space okay
                               }
               else if (c==’+’||c == '(' || c ==')' || c =='-')
                     {
                             // ()- okay too
                      }
                      else
                      {
                        return false;
                      }
               }
           //If not 14 digits then not a valid phone
                  if (numberCount != 14)
                   {

                   errors.add(field.getKey(),
                   Resources.getActionError(request,
                   va, field);

                   return false;
                   }
                   else
                   {
                    return true;
                    }
    {

    More JavaScript Articles
    More By A.P.Rajshekhar


       · HiThis is the last part of my three part article on Struts validators. Please...
       · You ARE indeed able to have 3 or more test cases represented in an validwhen...
     

    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 1 hosted by Hostway