Miscellaneous Code
  Home arrow Miscellaneous Code arrow Validate Class
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  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
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 CODE

Validate Class
By: lig
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2005-08-10

    Table of Contents:

    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


    * Class to validate user input
    A simple wrapper class for various validating techniques. uses regrex, ctype, date and string
    functions to get to a simple TRUE/FALSE answer. Class can be called without instantiation. Access with the CLASS::METHOD syntax.

    Example: if(!Validate::isAlNumUnder($user))<?php // vim: expandtab sw=4 ts=4 fdm=marker
    /**
    * Class to validate user input
    * A simple wrapper class for various validating techniques. uses regrex, ctype, date and string
    * functions to get to a simple TRUE/FALSE answer. Class can be called without instantiation.
    * Access with the CLASS::METHOD syntax.
    *
    * Example of usage:
    * // check to make sure the username fits the rules
    * if(!Validate::isAlNumUnder($user) || !Validate::isNotEmpty($user) || !Validate::validLength($user, 45))
    *
    *
    * The following work is licensed under a Creative Commons
    * Attribution-NonCommercial 2.5 License. For more
    * information on this license go to http://creativecommons.org/licenses/by-nc/2.5/
    */

    class Validate
    {
    /* {{{isNotEmpty */
    /**
    * Method to see if a value has been entered. The following are considered to be empty:
    *
    * "" (an empty string)
    * 0 (0 as an integer)
    * "0" (0 as a string) <--- Please note for when dealing with forms
    * NULL
    * FALSE
    * array() (an empty array)
    * var $var; (a variable declared, but without a value in a class)
    */
    function isNotEmpty($input)
    {
    return !empty($input);
    }
    /* }}}isNotEmpty */

    /* {{{isAlpha */
    /**
    * Method to check for all ASCII alphabetic characters (a-z A-Z). Blank
    * spaces and underscores are not allowed.
    */
    function isAlpha($input)
    {
    return ctype_alpha($input);
    }
    /* }}}isAlpha */

    /* {{{isAlphaNum */
    /**
    * Method to check for alpha-numeric characters only (a-z A-Z 0-9). Blank
    * spaces and underscores are not allowed.
    */
    function isAlphaNum($input)
    {
    return ctype_alnum($input);
    }
    /* }}}isAlphaNum */

    /* {{{isAlNumUnder */
    /**
    * Method to check for alpha-numeric and underscore characters. Blank spaces
    * aren't allowed.
    */
    function isAlNumUnder($input)
    {
    return preg_match('/^[a-zA-Z0-9_]+$/', $input)?TRUE:FALSE;
    }
    /* }}}isAlNumUnder */

    /* {{{isInteger */
    /**
    * Method to check for Integer characters (whole numbers only).
    */
    function isInteger($input)
    {
    return preg_match('/^[0-9]+$/',$input)?TRUE:FALSE;
    }
    /* }}}isInteger */

    /* {{{isFloat */
    /**
    * Method to check for a float value (decimal). Scientific notation is not
    * supported. A decimal point is required. See isInteger() if the decimal
    * is left out.
    */
    function isFloat($input)
    {
    return preg_match('/^[0-9]{1,10}[.][0-9]{1,10}$/', $input)?TRUE:FALSE;
    }
    /* }}}isFloat */

    /* {{{validTimestamp */
    /**
    * Method to make sure a timestamp is a valid date.
    */
    function validTimestamp($input)
    {
    return checkdate(date('n',$input), date('j', $input), date('Y', $input));
    }
    /* }}}validTimestamp */

    /* {{{validEmail */
    /**
    * Method to validate an email address. regrex pattern taken from regexlib.com
    * (http://www.regexlib.com/Default.aspx) submitted there by Myle Ott. allows
    * both IP addresses and regular domains.
    */
    function validEmail($input)
    {
    $pattern = '^([a-zA-Z0-9_\-])+(\.([a-zA-Z0-9_\-])+)*@((\[(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5]))\]))|((([a-zA-Z0-9])+(([\-])+([a-zA-Z0-9])+)*\.)+([a-zA-Z])+(([\-])+([a-zA-Z0-9])+)*))$';
    return preg_match( $pattern, $input)?TRUE:FALSE;
    }
    /* }}}validEmail */

    /* {{{validUrl */
    /**
    * Method to validate a full URL. Regex pattern copied from regrexlib.com
    * (http://www.regexlib.com/Default.aspx) no attribution was given for the
    * author. Iit will NOT match a valid URL ending with a dot or bracket
    */
    function validUrl($input)
    {
    $pattern = '^(http|https|ftp)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])*[^\.\,\)\(\s]$';
    return preg_match($pattern,$input)?TRUE:FALSE;
    }
    /* }}}validUrl */

    /* {{{validUSPhone */
    /**
    * Method to validate a US phone number. regex pattern taken off of
    * the PHP manual user comments for preg_match. Author is unknown.
    * Valid formats:
    *
    * (Xxx) Xxx-Xxxx
    * (Xxx) Xxx Xxxx
    * Xxx Xxx Xxxx
    * Xxx-Xxx-Xxxx
    * XxxXxxXxxx
    * Xxx.Xxx.Xxxx
    *
    */
    function validUSPhone($input)
    {
    return preg_match("/^(\(|){1}[2-9][0-9]{2}(\)|){1}([\.- ]|)[2-9][0-9]{2}([\.- ]|)[0-9]{4}$/", $input)?TRUE:FALSE;
    }
    /* }}}validUSPhone */

    /* {{{validDate */
    /**
    * Method to validate the combination of month, day and year. Leap
    * year is taken into account.
    */
    function validDate($month, $day, $year)
    {
    if(trim($month)>12 || trim($day)>31 || strlen(trim($year)) != 4)
    {
    return FALSE;
    }
    return checkdate($month, $day, $year);
    }
    /* }}}validDate */

    /* {{{validLength */
    /**
    * Method to validate the length of the string is less then the
    * maximum length.
    */
    function validLength($input, $max)
    {
    return (strlen($input) <= $max);
    }
    /* }}}validLength */
    }
    ?>

    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More Miscellaneous Code Articles
    More By lig

     

    IBM® developerWorks developerWorks - FREE Tools!


    Be the first to hear about i5/OS V6R1!

    Hold your calendar on January 30, 2008 for this free webcast on the new i5/OS. Rational's Enterprise Modernization products will be discussed at this webcast as they help to drive the application development environment for this new System i OS. <br />And learn how i5/OS will take you to the next step of efficient, resilient business processing. You will hear about the new i5/OS capabilities as it will be the most significant i5/OS release in years. If you cannot join the webcast on 1/30/08 you can still use this link to listen to the replay.<br />
    FREE! Go There Now!


    Check out the new Jazz space on developerWorks

    <a href="http://zeus.developershed.com/shonuff.php?blackbird=3853&zoneid=442&source=&dest=http%3A%2F%2Fwww.ibm.com%2Fdeveloperworks%2Fspaces%2Fjazz%3FS_TACT%3D105AGY31%26S_CMP%3DDEVSHED&ismap="><img src="http://images.devshed.com/corp/img/news/jazz01.gif" alt="developerWorks Jazz space" align="left"></a>You've heard the buzz about Jazz... want to know more about it from a developer's perspective? Check out the Jazz space on developerWorks. This space is an up-to-date resource for developers, including technical information about Jazz and products built on Jazz, like Rational Team Concert Express. The Jazz space includes content from a wide variety of sources, including links, feeds, and comments from experts.
    FREE! Go There Now!


    IBM – Taking Web 2.0 to Work

    You'll get answers to many questions and more from David Barnes, Lead Evangelist for IBM Emerging Internet Technologies. David will discuss aspects of Web 2.0 that bring value to corporations, academia, and government. He'll also discuss IBM's vision around Web 2.0, including the importance of remixability and consumability. The discussion will culminate with examples of various IBM Software Group solutions you can use to get ahead of the Web 2.0 adoption curve.
    FREE! Go There Now!


    NEW! Applying lean thinking to the governance of software development

    Effective governance for lean development isn’t about command and control. Instead, the focus is on enabling the right behaviors and practices through collaborative and supportive techniques. Hear from Scott Ambler on how it is far more effective to motivate people to do the right thing than it is to force them to do so. Learn how to form a lightweight, collaboration-based framework that reflects the realities of modern IT organizations.
    FREE! Go There Now!


    NEW! Evaluate Rational Host Access Transformation Services (HATS) Toolkit V7.1

    Visit IBM developerWorks to download a free trial of the Rational Host Access Transformation Services (HATS) Toolkit. The HATS toolkit provides a set of plug-ins for the IBM Rational Software Delivery Platform to help you easily extend your legacy applications. HATS makes your 3270 and 5250 applications available as HTML through the most popular Web browsers, while converting your host screens to a Web look and feel and it also enables you to develop new Web, portal, and rich-client applications.
    FREE! Go There Now!


    NEW! IBM Rational Systems Development e-Kit

    As systems increase in complexity, communication between systems and software teams becomes more and more difficult. Now, there’s a way to improve product quality and communication.<br />Read the “Model Driven Systems Development” white paper to see how. Also included in this kit are more educational white papers, customer examples, tutorials, informative Webcasts, and best practices for designing, building and managing systems.<br />
    FREE! Go There Now!


    NEW! Krugle, developerWorks, and code search

    Ken Krugler, co-founder of code search company Krugle, and Laura Merling, vice president of Marketing and Business Development for Krugle, join to talk about the ins and outs of code search and what it means as a new feature for developerWorks users.
    FREE! Go There Now!


    NEW! Project and Portfolio Management Executive Resource Kit

    Portfolio Management is about effectively managing portfolio value by aligning portfolio investments with business goals. This complimentary e-kit provides a collection of materials that can help you understand how IBM Rational enables and automates best practices for improved governance and clear visibility into portfolio and project performance across the entire IT project lifecycle.
    FREE! Go There Now!


    NEW! Rational Talks to You: Grady Booch on Architecture

    Join this Rational Talks to You teleconference on November 29 at 1:00 pm ET to participate in an interactive discusssion with Grady Booch around architecture and reuse. Get your questions answered!
    FREE! Go There Now!


    NEW! Whitepaper: Delivering SOA solutions: service lifecycle management

    The unprecedented scope of a service-oriented architecture (SOA) initiative brings to the forefront a number of management and governance issues that were sidestepped in the past. The key to a successful SOA implementation is managing and governing activities throughout the entire SOA delivery lifecycle by ensuring that services conform to the needs of all of the business’s stakeholders. Learn how service lifecycle management allows the business to ensure that the process by which services are defined, created, tested, deployed, optimized and retired is manageable, repeatable and auditable.
    FREE! Go There Now!



    All FREE IBM® developerWorks Tools!

    MISCELLANEOUS CODE ARTICLES

    - A Web App Based on a Model for the CodeIgnit...
    - Completing a Model for the CodeIgniter PHP F...
    - Validating Input Data with the CodeIgniter P...
    - Deleting Database Records with the CodeIgnit...
    - Inserting Database Records with a CodeIgnite...
    - Fetching Database Rows with a Model for the ...
    - Model Data and Validation Rules for a Generi...
    - Building a Generic Model for the CodeIgniter...
    - upload image to database sql
    - Random Password Generator
    - BCroot, get the root of a number with BC fun...
    - Find pi in a high precision
    - [PHP5] FORMCHECKER : data validation
    - SPL and ITERATOR : examples
    - Xml with Rss Feeds





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