PHP
  Home arrow PHP arrow Page 3 - Validating User Input with the Strateg...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
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? 
PHP

Validating User Input with the Strategy Pattern
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2007-03-06

    Table of Contents:
  • Validating User Input with the Strategy Pattern
  • Building a validation strategy selector
  • Building some strategy classes
  • Validating user-supplied data
  • Listing all the classes required to implement the strategy pattern

  • 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


    Validating User Input with the Strategy Pattern - Building some strategy classes


    (Page 3 of 5 )

    As you'll certainly recall from the previous section, the validation strategy selector spawns four different objects, and each one of them is responsible for validating a specific type of data. Therefore, taking this situation into account, below I listed the respective signatures corresponding to these strategy classes.

    Here they are, so have a look at them, please:

    // define 'StrategyAlphabetic' class
    class StrategyAlphabetic{
       public function validateData($inputData){
         if(!$inputData||!preg_match("/^[a-zA-Z]+$/",$inputData)){
           return false;
         }
         return true;
       }
    }
    // define 'StrategyAlphanumeric' class
    class StrategyAlphanumeric{
        public function validateData($inputData){
          if(!$inputData||!preg_match("/^[a-zA-Z0-9]
    +$/",$inputData)){
            return false;
          }
          return true;
        }
    }
    // define 'StrategyNumber' class
    class StrategyNumber{
       public function validateData($inputData){
         if(!$inputData||!is_numeric($inputData)){
           return false;
         }
         return true;
       }
    }
    // define 'StrategyEmail' class
    class StrategyEmail{
       public function validateData($inputData){
         if(!$inputData||!preg_match("/.+@.+..+./",$inputData)||!
    checkdnsrr(array_pop(explode("@",$inputData)),"MX")){
           return false;
         }
         return true;
       }          
    }

    As shown above, each of the four strategy classes listed previously implements differently the same "validateData()" method to check a diverse range of user inputs, such as alphabetic and alphanumeric values, and number and email addresses.

    Of course, it's possible to extend the initial validation capacity of the whole data checking system even more, simply by adding new strategy classes to the existing ones. This is definitely a process that can be performed with minor hassles.

    So far, so good. At this point, you've seen not only how the validation strategy selector looks, but how the four strategy classes have been properly defined. This is very convenient for understanding the schema imposed by the strategy pattern.

    So, the question that comes up is: what's the next step now? Well, considering that you already grasped the logic implemented by all the classes that I defined so far, it's time to move forward and see a concrete example where a data validation system is created by using the aforementioned classes.

    Want to see how the strategy pattern is applied to validate user-supplied input? Jump straight into the following section and keep reading.

    More PHP Articles
    More By Alejandro Gervasio


       · This final tutorial of the series is focused on demonstrating how to use the...
       · Thank you very much for all your nice articles that I read carefully and enjoy so...
       · Thank you for your kind comments on my PHP article, since it’s something that I...
       · Thank you for your quick reply.I am not really sure that OOP is more scalable...
       · Thank you again for commenting on my PHP article. Definitively, OOP is best suited...
     

       

    PHP ARTICLES

    - Using Aliases and the Autoload Function with...
    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Sub Classing Exceptions in PHP 5
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




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