|
|
|
| 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 |
 Dave Silvia |
| Date : |
Nov 10th 2006 |
| Grade : |
2 of 5 (graded 4 times) |
| Viewed : |
4066 |
| 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 |
|
|
|
|
|
|
|
|
|
| |
| <?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';
?> | | |
|
| Check for functional file links (broken Files)
Categories : PHP, Data Validation, FTP, Regexps, Arrays | | | Validating a URL with preg_match Categories : PHP, Regexps, Beginner Guides, Data Validation | | | Avoiding or Detecting high bit characters in a string. Useful when you want to create a valid RSS feed Categories : PHP, Strings, Unicode, Regexps, Rich Site Summary (RSS) | | | This script is a contact form between users of a
website (kinda like the PM function on the forums)
Categories : PHP, Databases, MySQL, Regexps | | | 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 | | | ereg -- Regular expression match Categories : PHP, PHP Functions, Regexps | | | Power Form Validation Categories : PHP, PHP Classes, Data Validation | | | Secure URL $_GET Categories : PHP, Data Validation, Security | | | Gets the browser and OS from the $_SERVER['http_user_agent'] variable in PHP Categories : PHP, HTTP, Regexps | | | BBCode Formatting String Categories : PHP, HTML, Regexps, Arrays | | | a PHP Function to Get only the filename (remove the extension) using regular expressions. Categories : PHP, Regexps, Beginner Guides | | | PHP Script to find url links in a page Categories : PHP, URLs, Regexps, Arrays | | | Tag content retrieval from websites with preg_match Categories : PHP, Regexps, Arrays, HTML and PHP | | | I need a trim function/regexp that will trim all " " from the ends of a string. Categories : Regexps, PHP, Strings | | | Db_lib - practical example usage of database abstraction and form validation.
Categories : PHP, Form Processing, PHP Classes, Data Validation, Beginner Guides | |
|
|
|