This class came about when I began to get heavily involved with uploading and working data for clients. I created another class that turns a csv file into it's own mysql database basically, and the need arose to separate my validation routines from the Form class they used to reside in.

These are the validation routines available:

'a' - An alpha (letters only) value
'an' - An alphanumeric (letters and numbers) value
'ap' - Letters and basic puncutuation
'anp' - An alphanumeric value with punctuation
'num' - A positive or negative number
'nummin' - A minimum number
'nummax' - A maximum number
'numrange' - A number within a range
'int' - A whole number greater than 0
'intmin' - A minimum integer
'intmax' - A maximum integer
'intrange' - An integer within a range
'float' - A positive number with a possible decimal (commas will be stripped)
'floatmin' - A minimum float
'floatmax' - A maximum float
'floatrange' - A float within a range
'string' - No extraneous spaces or code
'stringmin' - A string with a minimum number of characters
'stringmax' - A string with a maximum number of characters
'stringrange' - A string within a minimum and a maximum number of characters
'wordmin' - A string with a minimum number of words contained
'wordmax' - A string with a maximum number of words contained
'datetime' - A mysql formatted datetime value
'date' - A mysql formatted date in the format of 'Y-m-d'
'email' - A valid email address (according to syntax only)
'pass' - A password of at least 6 characters and no spaces
'url' - A valid website address including the 'http://' portion
'phone' - A 7 or 10 digit phone number
'zip' - A Canada, UK, or US zip code
'ssn' - A social security number
'cc' - A credit card number
'YN' - yes (Y) or no (N)
array() - An array of expected results such as array('yes', 'no', 'maybe', 'so'), etc.

I also added some other frequently used validation methods that allow you to:

  • Remove extra whitespace
  • Replace newlines with <br /> tags
  • Disable HTML and other markup
  • Properly capitalize titles
  • Nicely truncate a string
  • Generate an SEO value
  • Quickly columnize an array of data

Validating data can be an ugly uphill battle. This class makes it a breeze.

Creates a new validation object.

$validate = new Validation;

This method will filter and validate your data one value at a time. This is the method used in the Form class for all of the validation routines it performs.

$filter The type of data you're expecting. To include parameters make your filter an array. See examples.
$data The value you want to filter or validate.
Returns The value you expect (if possible), or nothing at all (if it's a string value). A number's default is 0, and the yes or no ('YN') default is 'N'. Credit card numbers include what type it is.
$filter = array(); 
$filter[] = 'a'; // alpha 
$filter[] = 'an'; // alphanumeric 
$filter[] = 'ap'; // alpha with punctuation 
$filter[] = 'anp'; // alphanumeric with punctuation 
$filter[] = 'num'; // a positive or negative number 
$filter[] = array('nummin', -16.23); // minimum number 
$filter[] = array('nummax', 42); // maximum number 
$filter[] = array('numrange', -16.23, 42); // number within a range 
$filter[] = 'int'; // a positive integer 
$filter[] = array('intmin', 2); // minimum integer 
$filter[] = array('intmax', 27); // maximum integer 
$filter[] = array('intrange', 2, 27); // integer within a range 
$filter[] = 'float'; // a positive float number 
$filter[] = array('floatmin', 3.14); // minimum float 
$filter[] = array('floatmax', 6.66); // maximum float 
$filter[] = array('floatrange', 3.14, 6.66); // float within a range 
$filter[] = 'string'; // a non-empty string 
$filter[] = array('stringmin', 4); // minimum string length 
$filter[] = array('stringmax', 100); // maximum string length 
$filter[] = array('stringrange', 4, 100) // a string within a range of characters 
$filter[] = array('wordmin', 2); // minimum number of words 
$filter[] = array('wordmax', 10); // maximum number of words 
$filter[] = 'datetime'; // a mysql formatted datetime value 
$filter[] = 'date'; // a mysql formatted date in the format of 'Y-m-d' 
$filter[] = array('date', 'm/d/Y'); // a formatted 'm/d/Y' date 
$filter[] = 'email'; // A valid email address (according to syntax only) 
$filter[] = 'pass'; // At least 6 characters and no spaces 
$filter[] = 'url'; // A valid website address including the 'http://' portion 
$filter[] = 'phone'; // A 7 or 10 digit phone number 
$filter[] = 'zip'; // A Canada, UK, or US zip code 
$filter[] = 'ssn'; // A social security number 
$filter[] = 'cc'; // A credit card number 
$filter[] = 'YN'; // yes (Y) or no (N) 
$filter[] = array('random', 'values', 'I', 'picked'); // value in_array() 
$filter[] = ''; // if blank (or not one of the above) we'll run it through $this->safe() 
foreach ($filter as $rule) { 
  $html .= $validate->data($rule, 'data') . '<br />'; 

This is for when you want to take a whole bunch of data and analyze each new line (or however else you want to delimit it), and just get the emails out of it for instance.

$filter The same rules and routines apply as in $this->data().
$data The data you want to work from. This data is ran through $this->strip($data) where all new lines are converted to <br /> tags, and every other tag is taken out.
$delimiter How you want to break up your $data for validation.
$return The default is whatever you used for the $delimiter. If you want to return your string in some other form then say so here.
Returns By default this method returns '<br />' separated values, and any duplicates found are taken out.
$data = 'an interesting line 
trash we don\'t care about 
$just_emails = $validate->multidata('email', $data);

This method is just like the strip_tags() function only we replace all newlines with '<br />' tags, and we remove any doubled-up whitespace and all tags (besides the ones you want to keep). This is usually only used when you're expecting a one-liner of information.

$data What you want to strip down.
$tags Any tags you want to keep.
Returns A stripped and sanitized string.
$string = $validate->strip('I am a messed     up string <b>that 
is difficult    to work          with</p>');

This method takes a string and sanitizes it basically. It replaces all newlines to '<br />' tags, and converts special characters to their HTML entities.

$data A string you're not too sure about.
Returns A string you can feel safe and secure with.
$string = $validate->safe('<script type="text/javascript"> alert("Hah! You\'ve been hacked"); </script> Hello, I am the bane of your existence.');

Performs proper capitalization on titles.

$string The title you would like to format.
Returns A properly capitalized title.
$validate->title('capitalize a title by force');

Truncates a bite out of text without cutting a word in half.

$string What you want to chop down to size
$max The maximum number of characters you would like to display.
$moretext How you want to indicate that the text has been truncated.
Returns Your truncated text.
$validate->truncate('Build a homeless man a fire and you will make him warm for a night. Set a homeless man on fire and he will be warm for the rest of his life.', 30);

When it comes to SEO friendly strings - less is more. This function will throw out the worthless words, accents and entities to give you something you can feed the bots. Mainly this is used to generate a search engine friendly url.

$string What you want to tell the search engines.
$implode How you want to tie the words together.
Returns Your SEO friendly string.
$seo = $validate->seo('Why isn\'t google sending me tons of free traffic?');

This will pad a multidimensional array of data to be displayed as columns.

$array A multidimensional array of data. See example.
$spacing How many spaces you would like between columns.
Returns A string of code to add to your $html.
$candidates = array(); 
$candidates[] = array('NUMBER', 'NAME', 'CHARACTER'); 
$candidates[] = array(4, 'LOCKE', 'John Locke'); 
$candidates[] = array(8, 'REYES', 'Hugo Reyes'); 
$candidates[] = array(15, 'FORD', 'James Ford'); 
$candidates[] = array(16, 'JARRAH', 'Sayid Jarrah'); 
$candidates[] = array(23, 'SHEPHARD', 'Jack Shephard'); 
$candidates[] = array(42, 'KWON', 'Jin and/or Sun Kwon '); 
$html .= $validate->columnize($candidates);

This will validate your reCAPTCHA and check for an appropriate response.

Returns An error string that will be empty if they entered the reCAPTCHA correctly.
$error = $validate->recaptcha();

This generates the jQuery code that will validate your form on the client side using the jQuery Validation plugin. It automatically (using the Page class) inserts the code into the head section of your page.

$form The name (id) of the form.
$check An array ('name'=>'filter') of the form values you want returned.
$required An array of required form input names.
$upload An array ('name'=>'types') of files to upload. 'Types' can also be it's own array.
$check = array('name'=>'', 'birthday'=>'date', 'email'=>'email', 'password'=>'pass'); 
$required = array('name', 'email', 'password'); 
$upload = array('pic'=>array('jpg', 'gif', 'png')); 
$validate->jquery('form', $check, $required, $upload);

This method validates and filters your form's submitted data.

$type A key=>value pair in the form of ($type=>$form) where $type is either 'get' or 'post', and $form is the form's name you want to validate.
$check An array ('name'=>'filter') of the form values you want returned.
$required An array of required form input names, ie. these fields must not be empty.
Returns An array of 3 values:
1) A $vars['name'] = $value array of all of the $form->check(), $form->process(), and $form->upload() methods previously called.
2) An $errors array that you don't need to worry about. You only need to know that it is empty which means it is safe to process all $vars.
3) An $eject string to know where to send the user so that they can't hurt themselves by resubmitting the same form over and over again when they click their browsers back button.
$type = array('post'=>'form'); 
$check = array('name'=>'', 'birthday'=>'date', 'email'=>'email', 'password'=>'pass'); 
$required = array('name', 'email', 'password'); 
list($vars, $errors, $eject) = $validate->form($type, $check, $required);

This processes a form's file field, and attempts to upload it to your uploads directory.

$file The name of file field you want to upload.
$types The type(s) of uploaded file(s) you're allowing. Available types include:
'jpg' - jpeg, jpg, pjpeg
'bmp' - bitmap
'swf' - flash
'doc' - ms word
'ppt' - powerpoint
'xls' - excel
'mpeg' - all types
'mov' - quicktime videos
Returns An array($filename, $error). If $filename !== false then this operation was successful. If (!empty($error)) then something went wrong, and we may have an idea of why.
list($filename, $error) = $validate->upload('image', array('jpg', 'gif', 'png')); 
if (!empty($filename)) { 
  // we can find this $filename in our uploads directory 
} elseif (!empty($error)) { 
  // houston, we have a problem