PHP
  Home arrow PHP arrow Page 3 - Building a PHP5 Form Processor: Coding the Form Validator Module
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PHP

Building a PHP5 Form Processor: Coding the Form Validator Module
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 12
    2006-01-23


    Table of Contents:
  • Building a PHP5 Form Processor: Coding the Form Validator Module
  • Checking for empty and integer values: looking at the “validateEmpty()” and “validateInteger()” methods
  • Testing numeric values and ranges: defining the “validateNumber()” and “validateRange()” methods
  • The big challenge: checking email addresses with the “validateEmail()” method
  • Checking for errors: defining the “checkErrors()” and displayErrors()” methods

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    Building a PHP5 Form Processor: Coding the Form Validator Module - Testing numeric values and ranges: defining the “validateNumber()” and “validateRange()” methods
    ( Page 3 of 5 )

    Indeed, another useful method to determine whether an user has entered a numeric value or not, is the “validateNumber()” method. Closely similar to the methods defined earlier, its source code looks like this:

    public function validateNumber($field,$errorMessage){
        if(!isset($_POST[$field])||!is_numeric($_POST[$field])){
            $this->errors[]=$errorMessage;
        }
    }

    In this case the method will check if the value entered is a numeric string and accordingly store the appropriate error message in the $errors array. Despite its simplicity, I’ve found myself very often using this method on forms where only numeric values are allowed.

    Now, take a look at another handy method, “validateRange()”, which can be very useful for checking if the value entered on a field is within a range of pre-defined boundaries. Its definition is listed below:

    public function validateRange
    ($field,$errorMessage,$min=1,$max=99){
        if(!isset($_POST[$field])||$_POST[$field]<$min||$_POST
    [$field]>$max){
            $this->errors[]=$errorMessage;
        }
    }

     

    Right, admittedly this method is very simple, so I won’t stop long on it. The only thing worthwhile of being denoted is the assignation of default values for the corresponding boundaries, which can be easily changed, in order to meet different validation requirements.

    In a similar fashion, if the input supplied by an user is out of the given boundaries, an error message will be added as a new element of the $errors array. As you can see, adding more validation methods to the class is just a matter of writing the appropriate checking routines and storing error messages according to the type of test performed on the input data.

    Having defined the methods responsible for checking numeric strings and ranges of values entered on forms, the next thing to be done is writing a couple more, so the class can be capable of validating alphabetic and alphanumeric strings.

    Validating alphanumeric and alphabetic values: defining the “validateAlphabetic()” and “validateAlphanum()” methods

    Checking for alphabetic and alphanumeric characters is really a straightforward task that can be easily performed by utilizing PHP regular expressions. Moreover, if you’ve been working with regular expressions for a while, you’ll realize that writing more methods to validate input that must meet a particular format is a breeze. Verifying postal codes or phone numbers is reduced to coding the corresponding string pattern. Based on this ability, the two methods listed below verify alphabetic and alphanumeric characters in input strings:

    public function validateAlphabetic($field,$errorMessage){
        if(!isset($_POST[$field])||!preg_match("/^[a-zA-Z]+$/",$_POST
    [$field])){
            $this->errors[]=$errorMessage;
        }
    }

    public function validateAlphanum($field,$errorMessage){
        if(!isset($_POST[$field])||!preg_match("/^[a-zA-Z0-9]
    +$/",$_POST[$field])){
            $this->errors[]=$errorMessage;
        }
    }

    Definitely you’ll agree that these methods are simple to code and read. This ability to use regular expressions to validate user input comes in particularly handy, since it’s possible to encode complex string patterns and perform strict validation on user-supplied data. As you can see, the above listed methods demonstrate this concept, by encoding simple string patterns in order to check for alphabetic and alphanumeric characters in input data. In both cases, if the input string fails the test, the respective error messages are stored in the $errors array, as shown in previous class methods.

    Certainly, I could spend more time coding additional methods to check whether input data fits a particular format. Since this process is reduced to tweaking string patterns in regular expressions, however, I won’t waste your precious time writing boring expressions. Instead, I’ll show you the last validation class method, which is quite useful for checking email addresses. So, jump into the next section to find out more about how this is done.



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek