Miscellaneous

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

    The first validation functions we will cover will do some very basic checks for us. We will validate if a variable contains only digits, only letters, and if it conforms to a certain length. These basic validation functions are useful on their own and as helper functions in other validation routines.

    Checking for Digits Only

    The first function we will look at determines if the string passed to it contains only numbers. We accomplish this by using a regular expression that encompasses the negating operator. If any character within the variable is a not a number, this function will return FALSE.

    <?php
    function isDigits($element) {
      return !
    preg_match ("/[^0-9]/"$element);
    }
    ?>

    Checking for Letters Only

    The next function we will examine is almost identical to the isDigits() function. This one, however, will verify if a string contains only letters. Like the isDigits() function, we use a regular expression with the negating operator to determine if any character in the string does not match the pattern we specify. If any character within the variable is not a letter, isLetters() will return FALSE.

    <?php
    function isLetters($element) {
      return !
    preg_match ("/[^A-z]/"$element);
    }
    ?>

    Checking a String for Length

    Now, let's look at a function that will check the length of a string and return FALSE if the length is not within a range we specify. For this task, we will make use of the strlen() function to determine the length of the string and then compare it to the specified minimum and maximum length.

    <?php
    function checkLength($string$min$max) {
      
    $length strlen ($string);
      if ((
    $length &lt$min) || ($length &gt$max)) {
        return 
    FALSE;
      } else {
        return 
    TRUE;
      }
    }
    ?>

    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