FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


PHP Validation Class





yimaw
I can't remember where I got this from but I use it all the time so I thought I shoud share it with you. Its written as a class so just put it in an external file, require the file and call the function, easy.

Put the below code in a file and name it Validator.php
Code:
<?php
class Validator {

    var $errors; // A variable to store a list of error messages

    // Validate something's been entered
    // NOTE: Only this method does nothing to prevent SQL injection
    // use with addslashes() command
    function validateGeneral($theinput,$description = ''){
        if (trim($theinput) != "") {
            return true;
        }else{
            $this->errors[] = $description;
            return false;
        }
    }
     
    // Validate text only
    function validateTextOnly($theinput,$description = ''){
        $result = ereg ("^[A-Za-z0-9\ ]+$", $theinput );
        if ($result){
            return true;
        }else{
            $this->errors[] = $description;
            return false;
        }
    }

    // Validate text only, no spaces allowed
    function validateTextOnlyNoSpaces($theinput,$description = ''){
        $result = ereg ("^[A-Za-z0-9]+$", $theinput );
        if ($result){
            return true;
        }else{
            $this->errors[] = $description;
            return false;
        }
    }
         
    // Validate email address
    function validateEmail($themail,$description = ''){
        $result = ereg ("^[^@ ]+@[^@ ]+\.[^@ \.]+$", $themail );
        if ($result){
            return true;
        }else{
            $this->errors[] = $description;
            return false;
        }
             
    }
     
    // Validate numbers only
    function validateNumber($theinput,$description = ''){
        if (is_numeric($theinput)) {
            return true; // The value is numeric, return true
        }else{
            $this->errors[] = $description; // Value not numeric! Add error description to list of errors
            return false; // Return false
        }
    }
     
    // Validate date
    function validateDate($thedate,$description = ''){

        if (strtotime($thedate) === -1 || $thedate == '') {
            $this->errors[] = $description;
            return false;
        }else{
            return true;
        }
    }
     
    // Check whether any errors have been found (i.e. validation has returned false)
    // since the object was created
    function foundErrors() {
        if (count($this->errors) > 0){
            return true;
        }else{
            return false;
        }
    }

    // Return a string containing a list of errors found,
    // Seperated by a given deliminator
    function listErrors($delim = ' '){
        return implode($delim,$this->errors);
    }
     
    // Manually add something to the list of errors
    function addError($description){
        $this->errors[] = $description;
    }     
         
}
?>

Then to use it where you are sending an input with the name "name" via POST:
Code:
<?php

require_once('Validator.php');

// Create an instance of Validator

$validator = new Validator();

// Will check that the input is TEXT ONLY, replace with validateNumber for a number etc
$validator->validateTextOnly($_POST['name'],'Your Name');
           if ( $validator->foundErrors() ){  // check if there are any errors
                         // Show any errors, with a line between each
            echo 'There was a problem with: <br>'.$validator->listErrors('<br />');
             
        }else{
            // Do whatever you want to do if its a good input.
        }
?>





The following is where you actually use the function. The bit that says validateTextOnly should be changed to match the function you want to use, so validateNumber if its a number. $_POST['name'] is the name of the variable you want to check and 'Your Name' should be changed to give the user a description of what they got wrong. This is probably best used with a "sticky" form to save users having to rely on the back button and/or having to enter all their details again as opposed to just the incorrect bits.
Code:
$validator->validateTextOnly($_POST['name'],'Your Name');



Have fun with it, any questions, just ask. I'd recommend using it whenever passing variables through the url (GET) to prevent SQL injection etc.
snowboardalliance
That looks good. Can you explain what exactly this validates though?
Related topics
[java scripts] Text effect , img ....
Dynamic PHP Images
Implementing your database access in PHP as a layer.
PHPBB
Frihost the solution for me?
PHP VS ASP
php coding needed
css right align
form mail problems (quality help=frih$)
PHP mailing script
How to override a:link style
XML problems combined with php midi class
PHP logging class
PHP Validation
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.