Miscellaneous

  Home arrow Miscellaneous arrow Page 6 - 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 6 of 14 )

    Validating email addresses can be tricky business. They come in many different forms and lengths. Luckily, we have regular expressions on our side so we should be able to differentiate between good and bad email addresses. Before we look at the function we will use to validate email addresses, let's take a look at some common forms of them so we know what we are dealing with.

    name@somwhere.com
    first.last@somewhere.com
    first.last@place.someplace.us
    my_name@me.info
    me.something@somewhere.co.uk

    As you can see, email addresses can come in all shapes and sizes. Now, let's look at the function, and then we will examine the regular expression piece by piece.

    <?php
    function checkEmail($email) {
      
    $pattern "/^[A-z0-9\._-]+"
             
    "@"
             
    "[A-z0-9][A-z0-9-]*"
             
    "(\.[A-z0-9_-]+)*"
             
    "\.([A-z]{2,6})$/";
      return 
    preg_match ($pattern$email);
    }
    ?>

    Now, let's take a closer look at the pattern line by line. The first line specifies the match should begin at the start of the string. Then, it matches any alphabetic character, any digit, a period, underscore, or a hyphen. These characters must occur at least one time, but may appear many times. That takes care of the user name portion of the email address. The next line matches the literal @ character and nothing else. The third line starts matching the host name part of the email address. First, it matches any alphabetic character or digit exactly once. Then, any alphabetic character, any digit, or a hyphen can be matched zero or more times.

    The fourth line starts by matching a period. It then goes on to match any alphabetic character, a digit, underscore, or hyphen at least once. This entire fourth line can be matched zero or more times.

    The last line of the pattern matches a period followed by a set of any alphabetic characters. The set should have between two and six characters. This will match the top-level domain, i.e., .com, .net, .uk, etc. At the time of this writing, all top-level domains are between two and six characters in length. With the addition of new top-level domains, however, this could change. Simply adjusting the range will take care of this problem should it occur.

    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 6 - Follow our Sitemap