WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
Internet Security Software
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : An array of functions to use in checking user input to HTML forms : text, firstName, middleNameOrInit, lastName, email, web, digits, decimal, hex, genNum, USD, BPS, Euro, USphone, USzip
Categories : PHP, Data Validation, Regexps Click here to Update Your Picture
Dave Silvia
Date : Nov 10th 2006
Grade : 2 of 5 (graded 5 times)
Viewed : 8830
File : 4526.php
Images : No Images for this code example.
Search : More code by Dave Silvia
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
 

<?php
/*
*  (c) 2006, D.E. Silvia, All rights reserved.
*  This code is available for use for non-commercial purposes.
*  Free to distribute as long as this copyright information remains intact.
*  No modification is authorized.  Please, refer bugs/enhancements to
*  dsilvia@mchsi.com
*
*/

/*
*
*  Array of functions to validate HTML form input. Input types are:
*
*     text              Any text
*     firstName         Person's first name
*     middleNameOrInit  Person's middle name or initial
*     lastName          Person's last name
*     email             Person's email address
*     web               Fully qualified http URL
*     digits            Input is digits (integer) only
*     decimal           Input is decimal with possible decimal point
*     hex               Input is hexidecimal
*     genNum            Input is a general number
*     USD               US Dollars with possible currency sign and decimal places
*     BPS               British Pounds Sterling with possible currency sign and decimal places
*     Euro              Ruro Dollars with possible currency sign and decimal places
*     USphone           US fully qualified phone number
*     USzip             US postal zip code
*
*  Functions check to see that there is input, then that the input
*  is of a valid form.
*
*  Calling syntax:
*
*      $isValid=$formValidator['text']($userText);
*      if($isValid)
*      {
*          // appropriate processing
*      }
*/

function validGenNum($var,$len)
{
    if(
IsEmpty($var)) return false;
    if(
preg_match('/^\d{'.$len.','.$len.'}(?:\-\d{4,4})?$/',$var)) return true;
    return
false;
}

function
validUSzip($var)
{
    if(
IsEmpty($var)) return false;
    if(
preg_match('/^\d{5,5}(?:\-\d{4,4})?$/',$var)) return true;
    return
false;
}

function
validUSphone($var)
{
    if(
IsEmpty($var)) return false;
    if(
preg_match('/^\d{10,10}$/',$var)) return true;
    return
false;
}

function
validFirstName($var)
{
    if(
IsEmpty($var)) return false;
    if(
preg_match('/^[A-Z][a-z\. ]*$/',$var)) return true;
    return
false;
}

function
validMiddleNameOrInit($var)
{
    return
validFirstName($var);
}

function
validLastName($var)
{
    if(
IsEmpty($var)) return false;
    if(
preg_match('/^[A-Z][A-Za-z\. \-]*$/',$var)) return true;
    return
false;
}

function
validEuro($var)
{
    if(
IsEmpty($var)) return false;
    if(
preg_match('/^€?\d+(?:\.\d{2,2})?$/',$var)) return true;
    return
false;
}

function
validBPS($var)
{
    if(
IsEmpty($var)) return false;
    if(
preg_match('/^£?\d+(?:\.\d{2,2})?$/',$var)) return true;
    return
false;
}

function
validUSD($var)
{
    if(
IsEmpty($var)) return false;
    if(
preg_match('/^\$?\d+(?:\.\d{2,2})?$/',$var)) return true;
    return
false;
}

function
validDigits($var)
{
    if(
IsEmpty($var)) return false;
    if(
is_numeric($var) && !preg_match('/[^\d]+/',$var)) return true;
    return
false;
}

function
validDecimal($var)
{
    if(
IsEmpty($var)) return false;
    if(
is_numeric($var) && !validHex($var) && preg_match('/[^\d]+/',$var)) return true;
    return
false;
}

function
validHex($var)
{
    if(
IsEmpty($var)) return false;
    if(
is_numeric($var) && preg_match('/[xX]+/',$var)) return true;
    return
false;
}

function
validWeb($var)
{
    if(
IsEmpty($var)) return false;
    if(!
preg_match('/^[Hh][Tt][Tt][Pp]:\/\/[\w\.\-]+[\w\.\-\/]+$/',$var)) return false;
    return
true;
}

function
validEmail($var)
{
    if(
IsEmpty($var)) return false;
    if(!
preg_match('/^[\w\.\-]+@[\w\.\-]+$/',$var)) return false;
    return
true;
}

function
validText($var)
{
    return !
IsEmpty($var);
}

function
IsEmpty($var)
{
    if(!isset(
$var)) return true;
   
$type=gettype($var);
    if(
$type == "string" && trim($var) == '') return true;
    if(
$type == "array" && $var == array()) return true;
    if(
$type == "object" && $var == (object)0) return true;
    if(
$type == "NULL") return true;
    return
false;
}

// validate there is input
$formValidator['text']='validText';

// validate input is alphabetic or dot
$formValidator['firstName']='validFirstName';

// validate input is alphabetic or dot
$formValidator['middleNameOrInit']='validMiddleNameOrInit';

// validate input is alphabetic or dot, space, or hyphen
$formValidator['lastName']='validLastName';

// validate input is 2 fields separated by 'at sign' (@) and
// fields are alphanumeric, underscore, dot, or hyphen
$formValidator['email']='validEmail';

// validate input begins with http (case insensitive) followed by a
// colon and 2 slashes with alphnumeric, underscore, dot, hyphen and
// possibly followed by an ending slash
$formValidator['web']='validWeb';

// validate input is integer
$formValidator['digits']='validDigits';

// validate input is float
$formValidator['decimal']='validDecimal';

// validate input is hexidecimal
$formValidator['hex']='validHex';

// validate input is all digits. has additional argument of exact length
$formValidator['genNum']='validGenNum';

// validate input is in US dollar format.
// possible leading '$' and possible trailing 2 place decimal.
$formValidator['USD']='validUSD';

// validate input is in British Pounds Steling format.
// possible leading '£' and possible trailing 2 place decimal.
$formValidator['BPS']='validBPS';

// validate input is in European Union Euro format.
// possible leading '€' and possible trailing 2 place decimal.
$formValidator['Euro']='validEuro';

// validate input is 10 decimal digits
$formValidator['USphone']='validUSphone';

// validate input is 5 decimal digits, possibly followed by '-' and 4 decimal digits
$formValidator['USzip']='validUSzip';
?>



Validating a URL with preg_match
Categories : PHP, Regexps, Beginner Guides, Data Validation
Banknote Validation - A PHP class that provides several methods to quickly validate banknote serial numbers of the following currencies: AUD, CAD, CHF, CNY, EUR, GBP, JPY, USD.
Categories : PHP, PHP Classes, Data Validation, Regexps
Check for functional file links (broken Files)
Categories : PHP, Data Validation, FTP, Regexps, Arrays
php table decoder used to convert an html table to individual tokens through regular expressions
Categories : PHP, Regexps, HTML and PHP
Simple way to replace a variable value in a .conf (.ini) file using a webbrowser - the first stage of a complete universal configuration editor
Categories : PHP, Regexps, Code Editors, Filesystem
columned txt file to array()?
Categories : Arrays, Strings, Regexps, PHP
ereg -- Regular expression match
Categories : PHP, PHP Functions, Regexps
Ping a Server and run a command to fix it if it is down
Categories : PHP, Errors and Logging, Regexps
Secure URL $_GET
Categories : PHP, Data Validation, Security
Power Form Validation
Categories : PHP, PHP Classes, Data Validation
Find if a year is leap.
Categories : PHP, Date Time, Beginner Guides, Data Validation
Clever Email Validation Function - E-Mail validation function with an eregi expression and socket connection.
Categories : Email, PHP, Regexps
Simple PHP program which calls other PHP program you can pass the variables to other PHP program : by Raju
Categories : PHP, PHP Options and Info, Regexps, Program Execution
Db_lib - practical example usage of database abstraction and form validation.
Categories : PHP, Form Processing, PHP Classes, Data Validation, Beginner Guides
Ensure that a specific value lies within a specific range.
Categories : PHP, Beginner Guides, Data Validation