Home arrow JavaScript arrow Page 3 - Validators: Concluding Remarks
JAVASCRIPT

Validators: Concluding Remarks


This article discusses two facets of the Struts-Validation framework. The first one deals with complex rules, while the second concerns information for creating custom rules that could be hooked up with the Validation framework.

Author Info:
By: A.P.Rajshekhar
Rating: 5 stars5 stars5 stars5 stars5 stars / 14
September 28, 2005
TABLE OF CONTENTS:
  1. · Validators: Concluding Remarks
  2. · Creating Custom Validators
  3. · Define the validation logic
  4. · Create an entry in the Validator-rules.xml

print this article
SEARCH DEVARTICLES

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;
                }
{


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-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials