Open Source PHP Data Validation Class

I have created a PHP5 class that handles all POST and GET variables. It performs almost all of the validations functions you could need. In addition, it makes accessing the variables very convenient.

Download here.
Version 1.0

How To use:
First, include the class and initiate it:




Optionally, you can use new Validation(false) to not parse all input data through the xss filter. This is not recommended.

Available functions are:

  • xss($string)
  • email($string)
  • phone($string)
  • url($string)
  • db_prep($string)

All of the above functions return a boolean value with the exception of xss() and db_prep(). Those two return a modified version of $string.

Here is an example of form validation:


getErrorMessage());}

include("validation.class.php");
$validation = new Validation;

echo "SELECT * FROM users WHERE user='".$validation->db_prep($validation->database_input)."'";
echo "";

if($validation->email($validation->email)) echo "valid email";
else echo "not valid email";
echo "";

if($validation->phone($validation->phone)) echo "valid phone";
else echo "not valid phone";
echo "";

if($validation->url($validation->url)) echo "valid url";
else echo "not valid url";
echo "";
?>
database input: email: phone: url:

A demo of the above test can be found here.

As you can see from the example above, instead of calling $_GET['username'], you can now call $validation->username without worrying about the data being ‘dirty’.

If you need help using this class, post a comment and I will gladly help you out. Also remember that this class is licensed under the Buy Me Dew License.

This entry was posted in PHP and tagged , , , , . Bookmark the permalink.

Comments are closed.