Validate

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hmvrulz
Forum Newbie
Posts: 20
Joined: Fri Oct 03, 2008 10:20 pm

Validate

Post by hmvrulz »

Code: Select all

class validate
{
 
    // Function to Sanatize the User Input
    function sanitize($input)
    {
        // Checks if the Input is an Array
        if (is_array($input)) {
            // Each layer of an array is Sanatized independently
            foreach ($input as $var => $val) {
                $output[$var] = $this->sanitize($val);
            }
        } else {
            // Checks for Magic Qoutes
            if (get_magic_quotes_gpc()) {
                // Strips Slashes of the input
                $input = stripslashes($input);
            }
            // Cleaning TAGS by calling the function
            $input = $this->clean_tags($input);
 
            // Adds Slashes for inserting into DATABASE
            $output = mysql_real_escape_string($input);
        }
 
        // Return the OUTPUT
        return $output;
    }
 
    // Strips HTML, JS, Style and Comments from the user Input
    function clean_tags($input)
    {
 
        // Set the Patterns
        $search = array('@<script[^>]*?>.*?</script>@si', // Strip out javascript
            '@<[\/\!]*?[^<>]*?>@si', // Strip out HTML tags
            '@<style[^>]*?>.*?</style>@siU', // Strip style tags properly
            '@<![\s\S]*?--[ \t\n\r]*>@' // Strip multi-line comments
            );
 
        // Strip the above patters
        $output = preg_replace($search, '', $input);
 
        // Return the OUTPUT
        return $output;
    }
 
    // Function to Desanatize the User Input
    function desanitize($input)
    {
        if (is_array($input)) {
            // Each layer of an array is Sanatized independently
            foreach ($input as $var => $val) {
                $output[$var] = $this->desanitize($val);
            }
        } else {
            // Checks for Magic Qoutes
            if (get_magic_quotes_gpc()) {
                // Strips Slashes of the input
                $output = stripslashes($input);
            }
        }
        // Returns an array Desanatized
        return $output;
 
    }
 
    // Function to Validate Email - International Format
    function email($email)
    {
        // Matches Email Id to Internation format
        if (preg_match('/^[_A-z0-9-]+((\.|\+)[_A-z0-9-]+)*@[A-z0-9-]+(\.[A-z0-9-]+)*(\.[A-z]{2,4})$/',
            $email)) {
 
            return true;
        } else {
 
            return false;
        }
    }
 
    // Method to Validate DATE - input MONTH,DAY,YEAR
    function ymd($month, $day, $year)
    {
        // Checks the date including Leap Year
        if (checkdate($month, $day, $year)) {
 
            return true;
        } else {
 
            return false;
        }
    }
 
    // Method to Check for Simple BLOG text
    function text($text)
    {
        $result = ereg("^[A-Za-z0-9\ ]+$", $text);
        if ($result) {
            return true;
        } else {
            return false;
 
        }
    }
 
    // Method to Check SINGLE WORD with A-Za-z0-9 with NO SPACES
    function alpha_num($text)
    {
        $result = ereg("^[A-Za-z0-9]+$", $text);
        if ($result) {
            return true;
        } else {
            return false;
 
        }
    }
 
    // Method to Check SINGLE WORD with only Characters with NO SPACES
    function alpha($text)
    {
        $result = ereg("^[A-Za-z]+$", $text);
        if ($result) {
            return true;
        } else {
            return false;
 
        }
    }
 
    // Method to Validate numbers only
    function num($input)
    {
        if (is_numeric($input)) {
            return true;
        } else {
            return false;
 
        }
    }
 
    // Validates 10 DIGIT mobile CODE
    function mobile($input)
    {
        if (is_numeric($input) && (strlen($input) == 10)) {
 
            return true;
 
        } else {
            return false;
        }
 
    }
 
    // MIN LENGTH of a string
    function minlength($input, $min)
    {
        if ((strlen($input) >= $min)) {
            return true;
        } else {
            return false;
        }
 
    }
 
    //Check Username
    function username($input)
    {
        // Checks if the Username is entered
        if (isset($input)) {
 
            // Checks if the Username contains alpha_num with out a scape or special Char
            if ($this->alpha_num($input)) {
 
                // Checks if the Username is on Min 3 Char
                if ($this->minlength($input, 3)) {
                    // Accepts the Input
                    return true;
 
                } else {
                    // Throws an Error
                    echo 'Min Length of Username is 3 Characters';
                    return false;
                }
            } else {
                // Throws an Error
                echo 'No Spaces or Special Characters Allowed!!!';
                return false;
            }
 
        } else {
 
            // Throws an Error
            echo 'Please enter a Username';
            return false;
        }
 
    }
 
    //Check Password
    function password($password1, $password2)
    {
        // Check if both Password fields are typed
        if ((isset($password1) and isset($password2))) {
 
            // Check if both the password fields match
            if ($password1 == $password2) {
 
                // Check for min Length
                if ($this->minlength($password1, 6) == true) {
 
                    // Check for Special chars and spaces
                    if ($this->alpha_num($password1) == true) {
 
                        // Accept Password
                        $password = $password1;
                        return true;
 
                    } else {
                        // Throws an Error
                        echo 'Password can contain A-Za-z0-9';
                        return false;
                    }
                } else {
                    // Throws an Error
                    echo 'Min Length 6 Char.';
                    return false;
                }
            } else {
                // Throws an Error
                echo 'Passwords dont match';
                return false;
            }
 
        } else {
            // Throws an Error
            echo 'You forgot to enter your password.';
            return false;
        }
    }
}
I have Created a VALIDATION CLASS... can plz tell if its the right way of doing it in OOPs. and have i left out any other USEFULL validation process
Post Reply