Here’s something I’m working on. Go ahead and critique it to your hearts desire. Any suggestions will be appreciated. (Yes I know it would be faster to just hard code the drop down list stuff instead of using a for loop on a multidimensional array… But I was being lazy =P)
[code=php]<?php
// Validation Class
// Validates and cleans user based input
// Ver 0.1
// Kenneth Ellis Mccall – August 8th 2007
//
// Functions:
// email():
// Validate an email address
//
// variables:
// $email: Email address.
// $level: Values 1, 2, 3 and 4.
// 1 = Simple regex checking. Default. (Fastest) — Need to add valid TLD’s (.com, .net)
// 2 = Checks to make sure the domain is valid and has an MX record. (Fast)
// 3 = Connects to the mail server and checks to if it really exsists. Doesn’t always work. (Slow)
// 4 = Check to see if the user does exist on the server. Doesn’t always work. (Slowest) — Not completed
//
// phone():
// Validate and clean phone number
//
// variables:
// $phone: Phone number.
// $level: Values 1 and 2.
// 1 = Simple check of US phone number – allow for ()’s, -‘s, spaces and .’s. — Regex isn’t 100% complete to what I want.
// 2 = Format to a simple 10 digit number.
//
// list_pre():
// Returns a form list element of name prefixes and also uses the SELECTED command to show the previous choice
//
// variables:
// $pre: Selection
//
// list_suf():
// Returns a form list element of name suffixes and also uses the SELECTED command to show the previous choice
//
// variables:
// $suf: Selection
//
// list_states():
// Returns a form list element of states and also uses the SELECTED command to show the previous choice
//
// variables:
// $state: Selection
//
// check_pre():
// check to make sure prefixes form element doesn’t have any injection.
//
// variables:
// $pre: prefix selection
//
// check_suf():
// check to make sure suffixes form element doesn’t have any injection.
//
// variables:
// $suf: suffix selection
//
// check_state
// check to make sure states form element doesn’t have any injection.
//
// variables:
// $state: state
//
// slash():
// Escapes strings – auto detects magic quotes and which escaping mechanism to use.
// Use stripslashes() to unescape.
//
// variables:
// $str: string
//
// fixword():
// Converts MS Word quotes and dashes to HTML
//
// variables:
// $str: string
class validation
{
var $states = array(‘AL’=>’Alabama’, ‘AK’=>’Alaska’, ‘AZ’=>’Arizona’, ‘AR’=>’Arkansas’, ‘CA’=>’California’, ‘CO’=>’Colorado’, ‘CT’=>’Connecticut’,
‘DE’=>’Delaware’, ‘DC’=>’District Of Columbia’, ‘FL’=>’Florida’, ‘GA’=>’Georgia’, ‘HI’=>’Hawaii’, ‘ID’=>’Idaho’, ‘IL’=>’Illinois’,
‘IN’=>’Indiana’, ‘IA’=>’Iowa’, ‘KS’=>’Kansas’, ‘KY’=>’Kentucky’, ‘LA’=>’Louisiana’, ‘ME’=>’Maine’, ‘MD’=>’Maryland’,
‘MA’=>’Massachusetts’, ‘MI’=>’Michigan’, ‘MN’=>’Minnesota’, ‘MS’=>’Mississippi’, ‘MO’=>’Missouri’, ‘MT’=>’Montana’, ‘NE’=>’Nebraska’,
‘NV’=>’Nevada’, ‘NH’=>’New Hampshire’, ‘NJ’=>’New Jersey’, ‘NM’=>’New Mexico’, ‘NY’=>’New York’, ‘NC’=>’North Carolina’,
‘ND’=>’North Dakota’, ‘OH’=>’Ohio’, ‘OK’=>’Oklahoma’, ‘OR’=>’Oregon’, ‘PA’=>’Pennsylvania’, ‘RI’=>’Rhode Island’, ‘SC’=>’South Carolina’,
‘SD’=>’South Dakota’, ‘TN’=>’Tennessee’, ‘TX’=>’Texas’, ‘UT’=>’Utah’, ‘VT’=>’Vermont’, ‘VA’=>’Virginia’, ‘WA’=>’Washington’,
‘WV’=>’West Virginia’, ‘WI’=>’Wisconsin’, ‘WY’=>’Wyoming’);
var $pre = array(‘Mr.’=>’Mr.’, ‘Mrs.’=>’Mrs.’, ‘Ms.’=>’Ms.’, ‘Miss’=>’Miss’,’Dr.’=>’Dr.’,’Rev.’=>’Rev.’, ‘Prof.’=>’Prof.’);
var $suf = array(‘0’=>’Select a suffix…’, ‘Sr.’=>’Sr.’, ‘Jr.’=>’Jr.’, ‘I’=>’I’, ‘II’=>’II’, ‘III’=>’III’, ‘IV’=>’IV’, ‘V’=>’V’, ‘M.D.’=>’M.D.’,
‘Ph.D.’=>’Ph.D.’, ‘M.A.’=>’M.A.’, ‘M.B.A.’=>’M.B.A’, ‘B.A.’=>’B.A.’, ‘B.S.’=>’B.S.’, ‘Esq.’=>’Esq.’,);
function email($email, $level=1)
{
$regex = “/^[a-z0-9]+([_\.-][a-z0-9]+)*@([a-z0-9]+([.-][a-z0-9]+)*)+\.[a-z]{2,}$/i”;
if(!preg_match($regex, $email))
{
return 0;
}
if($level > 1)
{
list($username,$domain)=split(‘@’,$email);
if(!checkdnsrr($domain,’MX’))
{
return 0;
}
}
if($level > 2)
{
if(!fsockopen($domain,25,$errno,$errstr,30))
{
return 0;
}
}
if($level > 3)
{
// Open a socket to the mailserver (MX), send ‘HELO’ – wait for the output (Test showed a long delay before
// output was seen. Then do ‘MAIL FROM: $email’ – wait for output and check for ‘250 ok’. Last – ‘RCPT TO: $email’ – wait
// for output – check for ‘250 ok’ (good) or ‘550 sorry, no mailbox here by that name. (#5.7.17)’
// The 3 digit code is what we are looking for – 250 is OK, 550 is ERROR. This will not work for on a lot of mail servers.
}
return 1;
}
function phone($phone, $level=1)
{
// Bokeh from WebDeveloper.com’s forums
$digits = preg_replace(‘/[^d]/’, ”, $phone);
if(strlen($digits) < 10 || strlen($digits) > 11)
{
return 0;
}
if($level > 1)
{
if(strlen($digits) == 11)
{
$digits = ltrim($digits, ‘1’);
}
return $digits; // Return cleaned output
}
return 1;
}
function list_pre($pre)
{
foreach($this->pre as $key=>$val)
{
$ret .= ‘<option value=”‘.$key.'”‘;
if($pre == $key)
{
$ret .= ‘ SELECTED’;
}
$ret .= ‘>’.$val.'</option>’;
}
return $ret;
}
function list_suf($suf)
{
foreach($this->suf as $key=>$val)
{
$ret .= ‘<option value=”‘.$key.'”‘;
if($suf == $key)
{
$ret .= ‘ SELECTED’;
}
$ret .= ‘>’.$val.'</option>’;
}
return $ret;
}
function list_states($state)
{
foreach($this->states as $key=>$val)
{
$ret .= ‘<option value=”‘.$key.'”‘;
if($state == $key)
{
$ret .= ‘ SELECTED’;
}
$ret .= ‘>’.$val.'</option>’;
}
return $ret;
}
function check_pre($pre)
{
if(!$this->pre[$pre])
{
return 0;
}
return 1;
}
function check_suf($suf)
{
if(!$this->suf[$suf])
{
return 0;
}
return 1;
}
function check_state($state)
{
if(!$this->states[$state])
{
return 0;
}
return 1;
}
function slash($str)
{
if(get_magic_quotes_gpc())
{
$str = stripslashes($str);
}
if(function_exists(‘mysql_real_escape_string’))
{
return mysql_real_escape_string($str);
}
elseif(function_exists(‘mysql_escape_string’))
{
return mysql_escape_string($str);
}
else
{
return addslashes($str);
}
}
function fixword($str)
{
// http://us.php.net/manual/en/function.utf8-decode.php#60109
$bad = array(“xe2x80x98”, // left single quote
“xe2x80x99”, // right single quote
“xe2x80x9c”, // left double quote
“xe2x80x9d”, // right double quote
“xe2x80x94”, // em dash
“xe2x80xa6” // elipses
);
$good = array(‘‘’,
‘’’,
‘“’,
‘”’,
‘—’,
‘…’);
return str_replace($bad, $good, $str);
}
}
?>