Jump to content

Validate


hmvrulz

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/129364-validate/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.