SimpleValidator (PHP)

Dealing with web forms? That means user generated content. That means you need to validate that data. It’s a real pain and unless you’re using a framework you’ll end up coding your own methods to validate email addresses, dates, phone numbers, zip codes, etc.

Enter SimpleValidator.

Full Documentation

Sample Usage

require "path/to/SimpleValidator.inc";
// array describing the keys to look for and their validation rules
// note that the 'required' rule can appear anywhere
// and that certain rules can accept arguments
$rules = array(
  'firstname'        => 'required alpha',
   'lastname'        => 'required',
      'phone'        => 'phone required',
       'test'        => 'minlength=5',
    'zipcode'        => 'zip',
    'password'       => 'required minlength=8',
    'password-again' => 'equalTo=password'
);
 
$validator = new SimpleValidator($rules);
// assuming $_POST contains the form data
// validate returns an array of error messages
$errors = $validator->validate($_POST);
 
if (empty($errors)) {
  // no errors in the data, processing can proceed
} else {
  // there are errors in the data
}

Leave a Reply

Powered by WP Hashcash

XyzzyBlog: all content is Public Domain unless noted otherwise Suffusion WordPress theme by Sayontan Sinha