Miscellaneous

  Home arrow Miscellaneous arrow Page 4 - Form and Spelling Validation
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

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    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

    blog comments powered by Disqus

    MISCELLANEOUS ARTICLES

    - Oracle Database XE: Indexes and Sequences
    - Modifying Tables in Oracle Database XE
    - Oracle Database XE: Tables and Constraints
    - More on Oracle Databases and Datatypes
    - Oracle Database XE Datatypes: Datetime and L...
    - Oracle Database XE Datatypes: Character and ...
    - From Databases to Datatypes
    - Firefox 3.6.6 Released with Improved Plug-in...
    - Attention Bloggers: WordPress 3.0 Now Releas...
    - Reflection in PHP 5
    - Inheritance and Other Advanced OOP Features
    - Advanced OOP Features
    - Linux from Scratch V.6.6 Review
    - Linux Gaining in Strength
    - Install Slackware on Your Old PC


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 3 - Follow our Sitemap