Most experienced coders will do their own custom validation. Good. This is aimed at newcomers and those who are frightened by regex
I'm sure there's plenty more I can add but these were all the things which sprung to mind as commony validated form inputs and I only put it together in the last hour (it's 3am
G'Night all
Code: Select all
<?php
/*
formValidator class for PHP written by d11wtq (regex lover)
Version: 0.1.0
Last Updated: 26th June 2005
*/
class formValidator {
var $m30 = array (
'04',
'06',
'09',
'11'
);
var $m31 = array (
'01',
'03',
'05',
'07',
'08',
'10',
'12'
);
var $m28 = '02';
var $months = array (
'JAN' => '01',
'FEB' => '02',
'MAR' => '03',
'APR' => '04',
'MAY' => '05',
'JUN' => '06',
'JUL' => '07',
'AUG' => '08',
'SEP' => '09',
'OCT' => '10',
'NOV' => '11',
'DEC' => '12'
);
/*
isName(). This is somewhat tricky to validate for ALL nationalities
-- don't use if you're not sure. There wasn't much point doing separate
-- methods for first and last name.
*/
function isName($input) {
if (preg_match('/^\w[a-z\-\']+$/i', $input)) return true;
return false;
} //End isFirstName
function isInt($input) {
if (preg_match('/^\d+$/', $input)) return true;
return false;
} //End isInt
function isFloat($input) {
if (preg_match('/^\d+(\.\d+)?$/', $input)) return true;
return false;
} //End isFloat
function isAllLetters($input) {
if (preg_match('/^[a-z]+$/i', $input)) return true;
return false;
} //End isAllLetters
function isLowercase($input) {
if (!preg_match('/[A-Z]/', $input) && preg_match('/[a-z]/', $input)) return true;
return false;
} //End isLowercase
function isUppercase($input) {
if (preg_match('/[A-Z]/', $input) && !preg_match('/[a-z]/', $input)) return true;
return false;
} //End isUppercase
function isCreditCard($input) {
if (preg_match('^\d{4}(?: )*\d{4}(?: )*\d{4}(?: )*\d{4}$/', $input)) return true;
return false;
} //End isCreditCard
/*
isTelephoneNumber(). No idea how this holds internationally I'm afraid
*/
function isTelephoneNumber($input) {
if (preg_match('/^(?:\+)?[\d ]$/', $input)) {
preg_match_all('/ /', $input, $matches);
$count = count($matches[0]);
if ($count > 4) {
return false;
} elseif ((strlen($input) - $count) < 10 || (strlen($input) - $count) > 15) {
return false;
} else {
return true;
} //End if
} //End if
return false;
} //End isTelephoneNumber
function isDDMMYYYY($input, $sep='/') {
$pattern = '/^(\\d{2})'.$sep.'(\\d{2})'.$sep.'(\\d{4})$/';
if (!preg_match($pattern, $input, $matches)) {
return false;
} else {
if (in_array($matches[2], $this->m30)) {
if ($matches[1] > 30 || $matches[1] < 1) return false;
return true;
} elseif (in_array($matches[2], $this->m31)) {
if ($matches[1] > 31 || $matches[1] < 1) return false;
return true;
} elseif ($matches[2] == $this->m28) {
if ($matches[1] > 29 || $matches[1] < 1) return false;
return true;
} else {
return false;
}
} //End if
} //End isDDMMYYYY
function isMMDDYYYY($input, $sep='/') {
$pattern = '/^(\\d{2})'.$sep.'(\\d{2})'.$sep.'(\\d{4})$/';
if (!preg_match($pattern, $input, $matches)) {
return false;
} else {
if (in_array($matches[1], $this->m30)) {
if ($matches[2] > 30 || $matches[2] < 1) return false;
return true;
} elseif (in_array($matches[1], $this->m31)) {
if ($matches[2] > 31 || $matches[2] < 1) return false;
return true;
} elseif ($matches[1] == $this->m28) {
if ($matches[2] > 29 || $matches[2] < 1) return false;
return true;
} else {
return false;
}
} //End if
} //End isMMDDYYYY
function isDDMMYY($input, $sep='/') {
$pattern = '/^(\\d{2})'.$sep.'(\\d{2})'.$sep.'(\\d{2})$/';
if (!preg_match($pattern, $input, $matches)) {
return false;
} else {
if (in_array($matches[2], $this->m30)) {
if ($matches[1] > 30 || $matches[1] < 1) return false;
return true;
} elseif (in_array($matches[2], $this->m31)) {
if ($matches[1] > 31 || $matches[1] < 1) return false;
return true;
} elseif ($matches[2] == $this->m28) {
if ($matches[1] > 29 || $matches[1] < 1) return false;
return true;
} else {
return false;
}
} //End if
} //End isDDMMYY
function isMMDDYY($input, $sep='/') {
$pattern = '/^(\\d{2})'.$sep.'(\\d{2})'.$sep.'(\\d{2})$/';
if (!preg_match($pattern, $input, $matches)) {
return false;
} else {
if (in_array($matches[1], $this->m30)) {
if ($matches[2] > 30 || $matches[2] < 1) return false;
return true;
} elseif (in_array($matches[1], $this->m31)) {
if ($matches[2] > 31 || $matches[2] < 1) return false;
return true;
} elseif ($matches[1] == $this->m28) {
if ($matches[2] > 29 || $matches[2] < 1) return false;
return true;
} else {
return false;
}
} //End if
} //End isMMDDYY
function isDMYYYY($input, $sep='/') {
$pattern = '/^(\\d{1,2})'.$sep.'(\\d{1,2})'.$sep.'(\\d{4})$/';
if (!preg_match($pattern, $input, $matches)) {
return false;
} else {
if (strlen($matches[1] < 2)) $matches[1] = '0'.$matches[1];
if (strlen($matches[2] < 2)) $matches[2] = '0'.$matches[2];
if (in_array($matches[2], $this->m30)) {
if ($matches[1] > 30 || $matches[1] < 1) return false;
return true;
} elseif (in_array($matches[2], $this->m31)) {
if ($matches[1] > 31 || $matches[1] < 1) return false;
return true;
} elseif ($matches[2] == $this->m28) {
if ($matches[1] > 29 || $matches[1] < 1) return false;
return true;
} else {
return false;
}
} //End if
} //End isDMYYYY
function isMDYYYY($input, $sep='/') {
$pattern = '^/(\\d{1,2})'.$sep.'(\\d{1,2})'.$sep.'(\\d{4})$/';
if (!preg_match($pattern, $input, $matches)) {
return false;
} else {
if (strlen($matches[1] < 2)) $matches[1] = '0'.$matches[1];
if (strlen($matches[2] < 2)) $matches[2] = '0'.$matches[2];
if (in_array($matches[1], $this->m30)) {
if ($matches[2] > 30 || $matches[2] < 1) return false;
return true;
} elseif (in_array($matches[1], $$this->m31)) {
if ($matches[2] > 31 || $matches[2] < 1) return false;
return true;
} elseif ($matches[1] == $this->m28) {
if ($matches[2] > 29 || $matches[2] < 1) return false;
return true;
} else {
return false;
}
} //End if
} //End isMDYYYY
function isDDMMMYYYY($input, $sep='/') {
$pattern = '/^(\\d{1,2})'.$sep.'(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)'.$sep.'(\\d{4})$/i';
if (!preg_match($pattern, $input, $matches)) {
return false;
} else {
if (!in_array($matches[2], $this->months)) return false;
$matches[2] = $this->$this->months[($matches[2])];
if (in_array($matches[2], $this->m30)) {
if ($matches[1] > 30 || $matches[1] < 1) return false;
return true;
} elseif (in_array($matches[2], $this->m31)) {
if ($matches[1] > 31 || $matches[1] < 1) return false;
return true;
} elseif ($matches[2] == $this->m28) {
if ($matches[1] > 29 || $matches[1] < 1) return false;
return true;
} else {
return false;
}
} //End if
} //End isDDMMMYYYY
function isMMMDDYYYY($input, $sep='/') {
$pattern = '/^(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)'.$sep.'(\\d{1,2})'.$sep.'(\\d{4})$/i';
if (!preg_match($pattern, $input, $matches)) {
return false;
} else {
if (!in_array($matches[1], $this->months)) return false;
$matches[1] = $this->$this->months[($matches[1])];
if (in_array($matches[1], $this->m30)) {
if ($matches[2] > 30 || $matches[2] < 1) return false;
return true;
} elseif (in_array($matches[1], $this->m31)) {
if ($matches[2] > 31 || $matches[2] < 1) return false;
return true;
} elseif ($matches[1] == $this->m28) {
if ($matches[2] > 29 || $matches[2] < 1) return false;
return true;
} else {
return false;
}
} //End if
} //End isMMMDDYYYY
function isUKPostcode($input) {
if (preg_match('/^[a-z]{1,2}\d{1,2}(?: )*[a-z]\d{1,2}$/i', $input)) return true;
return false;
} //End isUKPostcode
function isUSZipCode($input) {
if (preg_match('/^\d{5}([\- ]\d{4})?$/', $input)) return true;
return false;
} //End isUSZipCode
function isEmail($input) {
if (preg_match('/^[a-z0-9]+[\w\-_\.]*?[a-z0-9]@[a-z0-9]+[a-z0-9\-\.]*\.[a-z]{2,}$/i', $input)) return true;
return false;
} //End isEmail
/*
isEmailWithCommonTLD(). There's lots of TLD's... if what you need isn't validating,
-- simply add it to the final (?: ) list in the regex
*/
function isEmailWithCommonTLD($input) {
if (preg_match('/^[a-z0-9]+[\w\-_\.]*?[a-z0-9]@[a-z0-9]+[a-z0-9\-\.]*\.(?:com|uk|us|info|biz|gov|net|org|edu|ac|au|ca|de|eu|it|ro|ru|th)$/i', $input)) return true;
return false;
} //End isEmailWithCommonTLD
function isWebAddress($input) {
if (preg_match('@^https?\://[a-z0-9]+[a-z0-9\-\.]*?\.[a-z]{2,}(?:(?:\?|#).*?)@i', $input)) return true;
return false;
} //End isWebAddress
function isProtocol($input) {
if (preg_match('#^[a-z]+\://#i', $input)) return true;
return false;
} //End isInternetProtocol
function isIPAddress($input) {
if( preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/', $input)) return true;
return false;
} //End isIPAddress
}
?>