Miscellaneous
  Home arrow Miscellaneous arrow Page 4 - Form and Spelling Validation
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Forums Sitemap 
Dedicated Servers  
Download TestComplete 
JMSL Numerical Library 
IBM® developerWorks
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? 
MISCELLANEOUS

Form and Spelling Validation
By: Matt Wade
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 7
    2003-07-20

    Table of Contents:
  • Form and Spelling Validation
  • Common Form Validations
  • Spell Checking
  • Summary

  • 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


    Form and Spelling Validation -


    (Page 4 of 14 )

    Zip codes and postal codes can be an atrocious to validate. Each country has its own system of postal codes. For the purposes of this lesson, we will examine how to validate US zip codes and Canadian postal codes. Many other countries follow the same format as the US and Canada does. Other places, such as the United Kingdom, have very different formats.

    The function we are about to examine for zip and postal code validation is written in such a way that other countries can be plugged into it, so that we can expand on the function if the need arises.

    US zip codes come in two different flavors, a standard zip code that is five digits or zip+4 which is 5 digits, a hyphen, and then 4 more digits. We will account for both of these possibilities. The first thing we will do with any zip code passed to the function is to remove all spaces and hyphens. This will allow us to examine the zip code without worrying if the user included a hyphen or a space. The next step in validating the US zip code is to check its length. Acceptable lengths would be five or nine digits once spaces and hyphens are removed. Any zip code that does not meet the length standard will not validate. The final check will be to make sure that the zip code only contains digits. For this, we will use the isDigits() function from the previous section.

    Validating Canadian postal codes follows a similar route as the US zip code. First, all spaces or hyphens will be removed and then the length of the postal code will be examined. A Canadian postal code should always be six characters in length. After the length check, we will check to make sure the postal code fits the proper pattern. The postal code should consist of three sets of a letter followed by a number.

    Now, let's look at the function we will use to accomplish the validation.

    <?php
    function checkMailCode($code$country) {
      
      
    $code preg_replace("/[\s|-]/"""$code);
      
    $length strlen ($code);

      switch (
    strtoupper ($country)) {
        case 
    'US':
          if ((
    $length &lt;&gt5) &amp;&amp; ($length &lt;&gt9)) {
            return 
    FALSE;
          }
          return 
    isDigits($code);
        case 
    'CA':
          if (
    $length &lt;&gt6) {
            return 
    FALSE;
          }
          return 
    preg_match ("/([A-z][0-9]){3}/"$code);
      }
    }
    ?>

    The great thing about the structure of this function is that if you need to validate other countries, you can drop the routines for them right into it with little hassle. Another method of expanding this function is to just add cases for countries that already match one of the existing ones. For example, Mexico used a five-digit zip code like the US. To add Mexico to this function, you would only need to add one line, directly under the case for the US.

    <?php
    switch (strtoupper ($country)) {
        case 
    'US':
        case 
    'MX':
          if ((
    $length &lt;&gt5) &amp;&amp; ($length &lt;&gt9)) {
    ?>

    More Miscellaneous Articles
    More By Matt Wade


       · 
       · Comments given fo title
       · sorry I ran this function and itdidn't work.
       · Remove the echo statement.
       · td.error {color:C03; font-weight:bold; }is incorrect, should be:td.error...
       · td.error {color: red; font-weight:bold; }
       · :)
       · I dont know how this is supposed to work but it doesnt..I can get the spell...
       · HOLY CRAP! I have been stuck on this for about a week and have spent about 6-10...
     

    MISCELLANEOUS ARTICLES

    - Stopping CSRF Attacks in Your PHP Applicatio...
    - Quick and Dirty AJAX Tutorial
    - Flickr Puzzle Mashup
    - The PAVISE of Security
    - Creating a CAPTCHA with PHP
    - Sending SMS Thru HTTP
    - The Postal Fix - Part 2
    - Adding Mail with Exim
    - The Postal Fix - Part 1
    - Create Your Own Custom API
    - Adding Drop Shadows with PHP
    - Writing a Basic Authentication System in PHP
    - Overlapping Images with GD
    - Using Sockets in PHP
    - Dynamic CSS with PHP






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway