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: 4 stars4 stars4 stars4 stars4 stars / 9
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

TOOLS YOU CAN USE

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


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



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