* 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
developerWorks - FREE Tools! |
<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!
|
|
|
|
Attend this launch webcast with Scott Hebner, Vice President of IBM Rational Marketing and Strategy, for an overview of Rational’s new software offerings and resources to help modernize and accelerate software innovation on i on Power Systems – while ensuring past application investments are protected and continue to grow. Learn how these solutions are helping customers extend their core i5/OS solutions toward modern architectures such as SOA and web technologies to deliver business improvements that stand the test of time. FREE! Go There Now!
|
|
|
|
You probably have thousands of lines of COBOL code loaded with business intelligence and being used to run your business, along with an army of developers maintaining these applications. Learn how to prepare your applications and developers so you can keep that competitive edge and move to a service-oriented architecture with the IBM Rational Enterprise Modernization solutions. Replay is available for 9 months. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download IBM DB2 Express-C 9.5, a no-charge version of DB2 Express 9 database server. DB2 Express-C offers the same core data server base features as other DB2 Express editions and provides a solid base to build and deploy applications developed using C/C++, Java, .NET, PHP, and other programming languages. FREE! Go There Now!
|
|
|
|
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!
|
|
|
|
Listen to this webcast to get an overview of Info 2.0 and a technical demo of how to quickly build an enterprise mashup. IBM's Info 2.0 technology leverages emerging Web 2.0 technologies such as mashups, feeds, AJAX, and JSON in order to simplify assembly of information using feeds and services. Come learn about the technical elements of Info 2.0 including the Feed Generation framework, Mashup Engine, and mashup assembly components. Learn how to pull information from databases, departmental information, and the Web to create mashups critical to your company’s success. We will also discuss best practices to help you get started. FREE! Go There Now!
|
|
|
|
Learn the basics of the IBM Customer Information Control System (CICS). With a hands-on exercise, learn how to get your first CICS application up and running on your desktop using TXSeries V6.1 for Windows. The tutorial shows you how to download and install a free trial version of TXSeries V6.1. FREE! Go There Now!
|
|
|
|
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!
|
|
|
|
Explore how Rational and WebSphere software enable enterprise documentation in SOA environments. Specifically, a new integration between IBM WebSphere® Business Modeler and IBM Rational® Method Composer software can help technical writers more easily keep enterprise operations manuals in sync with changes that are made to business processes, resulting in more accurate and timely documentation that benefits the entire enterprise. FREE! Go There Now!
|
|
|
|
With IBM Rational Systems Development Solution, you can deliver products faster with higher quality. Within this kit, Read the “Model Driven Systems Development” white paper to see how to improve product quality and communication. Then check out the rest of the e-Kit to learn more about important topics that can affect the success of any software project through customer examples, tutorials, informative Webcasts, and best practices for designing, building and managing systems. From start to finish, at every stage in your projects, Rational Systems Development Solution can help your company reach its full potential. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |