Easy PHP Validation

Description

This class encapsulates various validation functionality.

PHP developers should always validate data on the server side to prevent Problems. JavaScript alone is not a good idea for data validation and security.

Feb 20, 2007 - 1.0 Initial Release
Feb 28, 2007 - 1.1 Vitaliy Bogdanets contributed regular expressions
Mar 12, 2007 - 2.0 Restructured/renamed class, made static, added functionality
Mar 27, 2007 - Added getBooleanValue method
Jun 19, 2007 - Added functionality and bug fix for getBooleanValue
Jun 29, 2007 - Changed checkMinimumLength to isTooShort and fixed help

  • author: Jeff L. Williams
  • version: 2.3

Located at http://www.phpclasses.org/browse/package/3737.html

Usage:
include("validation.class.php");

Method Summary
boolean checkLength (string $value, integer $maxLength, integer $minLength)
boolean compare (string $value1, string $value2, [boolean $caseSensitive = false])
boolean getBooleanValue (any $value, [any $includeTrueValue = null], [any $includeFalseValue = null])
string getCookieValue (string $name, [string $default = ''])
string getDefaultOnEmpty (mixed $value, string $default, string $name)
string getFormValue (string $name, [string $default = ''])
string getSessionValue (string $name, [string $default = ''])
string getValue (string $name, [string $default = ''])
boolean hasValue (string $value)
boolean isAlpha (string $value, [string $allow = ''])
boolean isAlphaNumeric (string $value)
boolean isDate (string $value)
boolean isEmail (string $email)
boolean isEmpty (string $value)
boolean isInternetURL (string $value)
boolean isIPAddress (string $value)
boolean isNumber (integer $number)
boolean isStateAbbreviation (string $value)
boolean isTooLong (string $value, integer $maximumLength)
boolean isTooShort (string $value, integer $minimumLength)
boolean isUnsignedNumber (integer $number)
Methods
checkLength

Checks the length of a value

  • return: TRUE if the requirements are met, FALSE if not
  • static:
boolean checkLength (string $value, integer $maxLength, integer $minLength)
  • string $value: The value to check
  • integer $maxLength: The maximum allowable length of the value
  • integer $minLength: [Optional] The minimum allowable length

Example:

//Check for the length of a string
$test = "abc";
if (valid::checkLength($test, 3, 2)) {
    echo "good length";
}
else {
    echo "bad length";
}
compare

Compares two values for equality

  • return: TRUE if the values are equal and FALSE if not
  • static:
boolean compare (string $value1, string $value2, [boolean $caseSensitive = false])
  • string $value1: First value to compare
  • string $value2: Second value to compare
  • boolean $caseSensitive: [Optional] TRUE if compare is case sensitive

Example:

//Compare two values
$test1 = "apples";
$test2 = "oranges";
if (valid::compare($test1, $test2)) {
    echo "match";
} else {
    echo "no match";
}
getBooleanValue

Converts any value of any datatype into boolean (true or false)

  • return: Returns TRUE or FALSE
  • access: public
boolean getBooleanValue (any $value, [any $includeTrueValue = null], [any $includeFalseValue = null])
  • any $value: Value to analyze for TRUE or FALSE
  • any $includeTrueValue: (Optional) return TRUE if the value equals this
  • any $includeFalseValue: (Optional) return FALSE if the value equals this

Example:

//Convert to a boolean value
$test = "Yes";
if (valid::getBooleanValue($test)) {
    echo "Evaluates to TRUE";
}
else {
    echo "Evaluates to FALSE";
}
getCookieValue

Get the value for a cookie by the cookie name

  • return: The cookie value
  • static:
string getCookieValue (string $name)
  • string $name: The name of the cookie
  • string $default: [Optional] A default if the value is empty

Example:

//Get a cookie without errors or isset
echo "Cookie: " . valid::getCookieValue("CookieName");
getDefaultOnEmpty

Returns the value if one exists, otherwise returns a default value (This also works on NULL values)

  • return: Returns the original value unless this value is empty - in which the default is returned
  • static:
  • access: public
string getDefaultOnEmpty (mixed $value, string $default, string $name)
  • string $name: The value to check
  • string $default: A default if the value is empty

Example:

//If the value is empty, blank, or null, the give a default value
echo valid::getDefaultOnEmpty(null, "NULL Value");
getFormValue

Get a POST or GET value by a form element name

  • return: The value of the form element
  • static:
string getFormValue (string $name)
  • string $name: The name of the POST or GET data
  • string $default: [Optional] A default if the value is empty

Example:

//Get a POST or GET the same way without errors or isset
echo "POST/GET: " . valid::getFormValue("NameOfFormElement");
getSessionValue

Get the value for a session by the session name

  • return: The session value
  • static:
string getSessionValue (string $name)
  • string $name: The name of the session
  • string $default: [Optional] A default if the value is empty

Example:

//Get a session variable without errors or isset
echo "Session: " . valid::getSessionValue("VariableName");
getValue

Get a POST, GET, Session, or Cookie value by name (in that order - if one doesn't exist, the next is tried)

  • return: The value from that element
  • static:
string getValue (string $name)
  • string $name: The name of the POST, GET, Session, or Cookie
  • string $default: [Optional] A default if the value is empty

Example:

//Get a cookie, post, get, or session variable without errors or isset
echo valid::getValue("ValueName");
hasValue

Checks to see if a variable contains a value

  • return: TRUE if a value exists, FALSE if empty
  • static:
boolean hasValue (string $value)
  • string $value: The value to check

Example:

//Test to see if a value has been set or is blank
$test = null;
if (valid::hasValue($test)) {
    echo "a value exists";
}
else {
    echo "no value";
}
isAlpha

Determines if a string is alpha only

  • static:
boolean isAlpha (string $value, [string $allow = ''])
  • string $value: The value to check for alpha (letters) only
  • string $allow: Any additional allowable characters

Example:

//Check for alpha only (no numbers or symbols)
$test = "abcdefg";
if (valid::isAlpha($test)) {
    echo "alpha";
}
else {
    echo "not alpha";
}
isAlphaNumeric

Determines if a string is alpha-numeric

  • return: TRUE if there are letters and numbers, FALSE if other
  • static:
boolean isAlphaNumeric (string $value)
  • string $value: The value to check

Example:

//Check for alpha numeric
$test = "abc123";
if (valid::isAlphaNumeric($test)) {
    echo "alpha-numeric";
}
else {
    echo "not alpha-numeric";
}
isDate

Determines if a string contains a valid date

  • return: TRUE if the value is a date, FALSE if not
  • static:
boolean isDate (string $value)
  • string $value: The value to inspect

Example:

//Check for a valid date
$test = "March 1, 2007";
if (valid::isDate($test)) {
    echo "date";
}
else {
    echo "not date";
}
isEmail

Checks for a valid email address

  • return: TRUE if it is a valid email address, FALSE if not
  • static:
boolean isEmail (string $email)
  • string $email: The value to validate as an email address

Example:

//Check for a valid email address
$test = "some.person@email.com";
if (valid::isEmail($test)) {
    echo "email";
}
else {
    echo "not email";
}
isInternetURL

Checks for a valid internet URL

  • return: TRUE if the value is a valid URL, FALSE if not
  • static:
boolean isInternetURL (string $value)
  • string $value: The value to check

Example:

//Check for a valid URL
$test = "http://www.google.com";
if (valid::isInternetURL($test)) {
    echo "URL";
}
else {
    echo "not URL";
}
isIPAddress

Checks for a valid IP Address

  • return: TRUE if the value is an IP address, FALSE if not
  • static:
boolean isIPAddress (string $value)
  • string $value: The value to check

Example:

//Check for a valid IP address
$test = "10.20.30.40";
if (valid::isIPAddress($test)) {
    echo "IP address";
}
else {
    echo "not IP address";
}
isNumber

Checks to see if a variable is a number

  • return: TRUE if the value is a number, FALSE if not
  • static:
boolean isNumber (integer $number)
  • integer $number: The value to check

Example:

//Check for a number
$test = "12345";
if (valid::isNumber($test)) {
    echo "number";
}
else {
    echo "not number";
}
isStateAbbreviation

Checks for a two character state abbreviation

  • return: TRUE if the value is a 2 letter state abbreviation FALSE if the value is anything else
  • static:
boolean isStateAbbreviation (string $value)
  • string $value: The value to inspect

Example:

//Check for a state code
$test = "NY";
if (valid::isStateAbbreviation($test)) {
    echo "state code";
}
else {
    echo "not state code";
}
isTooLong

Check to see if a string length is too long

  • return: TRUE if the length is too long FALSE if the length is acceptable
  • static:
  • access: public
boolean isTooLong (string $value, integer $maximumLength)
  • string $value: The string value to check
  • integer $maximumLength: The maximum allowed length

Example:

//Check to see if this string is too long
$test = "abcd";
if (valid::isTooLong($test, 3)) {
    echo "too long";
}
else {
    echo "good length";
}
isTooShort

Check to see if a string length is too short

  • return: TRUE if the length is too short FALSE if the length is acceptable
  • static:
  • access: public
boolean isTooLong (string $value, integer $maximumLength)
  • string $value: The string value to check
  • integer $minimumLength: The minimum allowed length

Example:

//Check to see if this string is too long
$test = "abcd";
if (valid::isTooShort($test, 5)) {
    echo "too short ";
}
else {
    echo "good length";
}
isUnsignedNumber

Checks to see if a variable is an unsigned number

  • return: TRUE if the value is a number without a sign and FALSE if a sign exists
  • static:
boolean isUnsignedNumber (integer $number)
  • integer $number: The value to inspect

Example:

//Check for an unsigned number (no minus sign)
$test = "-123.45";
if (valid::isUnsignedNumber($test)) {
    echo "unsigned number";
}
else {
    echo "not unsigned number";
}