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  
Forums Sitemap 
Dedicated Servers  
Download TestComplete 
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 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    * 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!


    NEW! Evaluate IBM Rational Software Analyzer V7.0

    Download a free trial version of IBM Rational Software Analyzer Developer Edition V7.0 to identify bug defects earlier in the software development cycle. Rational Software Analyzer is an extensible software development solution that reduces the expense of bug-fixes by enabling static analysis code reviews and bug identification very early in the development cycle.
    FREE! Go There Now!


    NEW! Evaluate Rational Business Developer V7.1

    Visit IBM developerWorks to download a free trial version of IBM Rational Business Developer V7.1. Rational Business Developer offers rapid and simplified development of business applications and services through Enterprise Generation Language (EGL) tools, generating Java or mainframe solutions while shielding developers from technical complexities.
    FREE! Go There Now!


    NEW! Hacking 101

    Join us for this web seminar to learn how you can defend your web applications from attack. Learn about the 3 most common web application attacks, including how they occur and what can be done to prevent them. We’ll also discuss manual versus automated approaches for scanning and identifying web application vulnerabilities and how IBM Rational AppScan, an automated vulnerability scanner, can help you automate more of what you are doing manually today.
    FREE! Go There Now!


    NEW! Rational Talks to You:Per Kroll on Rational Method Composer Plug-in customization

    Join this Rational Talks to You teleconference on December 11 at 1:00 pm ET to get tips on building your own plugins with Rational Method Composer. Get your questions answered!
    FREE! Go There Now!


    NEW! The role of integrated requirements management in software delivery

    This paper is about the critical role that a discipline called integrated require­ments management can play in helping to ensure that your business goals and IT investments are continuously aligned—whether you are sourcing, integrat­ing, building or maintaining software. It also looks at ways that automated IBM Rational® products can work together to help you use requirements in the very best way.
    FREE! Go There Now!


    NEW! Trial download: IBM Rational Functional Tester V7.0.1

    Get a free trial download of the latest version of IBM Rational Functional Tester V7.0.1. Rational Functional Tester is an automated functional and regression testing solution for QA teams concerned with the quality of their Java, Microsoft Visual Studio .NET, and Web-based applications.
    FREE! Go There Now!


    NEW! Understanding Web application security challenges

    As businesses grow increasingly dependent upon Web applications, these complex entities grow more difficult to secure. Most companies equip their Web sites with firewalls, Secure Sockets Layer (SSL), and network and host security, but the majority of attacks are on applications themselves – and these technologies cannot prevent them. This paper explains what you can do to help protect your organization, and it discusses an approach for improving your organization’s Web application security.
    FREE! Go There Now!


    NEW! Webcast: Calling All Testers! Find Application Vulnerabilities Early in the Development Process Where they are Easier to Fix and Less Risky to your Business

    In this webcast, IBM Rational will discuss the importance of Web application security and will share techniques and best practices to introduce application security testing into current QA processes including: understanding common security vulnerabilities and techniques to integrate security testing with defect tracking and remediation systems in an effort to safeguard sensitive online information.
    FREE! Go There Now!


    NEW! Webcast: Extreme transaction processing with WebSphere Extended Deployment

    In this webcast, you'll get an introduction to the eXtreme Transaction Processing (XTP) features of WebSphere Extended Deployment and the common architectural traits required by XTP applications. See how WebSphere Extended Deployment's ObjectGrid feature provides a state-of-the-art infrastructure for hosting XTP applications.
    FREE! Go There Now!


    NEW! Webcast: Introducing the new Information Server and Solutions community: LeverageInformation

    User communities play an important role in communication and collaboration around products, solutions and other areas of special interest to members. Successful communities are able to provide the right mix of content and services to deliver a value proposition that resonates with each audience. Join Tom Inman, VP of Marketing for Information and Platform Solutions as he introduces the new LeverageINFORMATION community. During this webcast, learn about the value provided by the community and how customers and partners derive value from the community in addressing their own technical and business challenges.
    FREE! Go There Now!



    All FREE IBM® developerWorks Tools!

    MISCELLANEOUS CODE ARTICLES

    - 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
    - [PHP5] NOTIMEOUT PACKAGE
    - Class to return variables to a Flash movie.
    - BCGCD Greatest Common Denominator (Large Num...
    - HMAC
    - Binary to Decimal
    - Decimal to Binary using logs
    - web.framework v1.0.0
    - Shopping Cart Class






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