Location: PHPKode > scripts > MySQLConnection > mysqlconnection/validator.php
<?php
class Validator {
	var $validation_error;
	
	function Validator(&$fields_values) {
		foreach ($fields_values as $key => $value) {
			// validates a required field
			if (array_key_exists("{$key}_required", $fields_values)) {
				if (!strlen(trim($value))) {
					trigger_error($fields_values["{$key}_required"]);
					$this->validation_error = true;
				}
			}

			// validates an integer field
			if (array_key_exists("{$key}_integer", $fields_values)) {
				if (!is_numeric($value) && strlen(trim($value))) {
					trigger_error($fields_values["{$key}_integer"]);
					$this->validation_error = true;
				}
			}

			// validates an email address
			if (array_key_exists("{$key}_email", $fields_values)) {
				if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,5})$/", strtolower($value)) && strlen(trim($value))) {
					trigger_error($fields_values["{$key}_email"]);
					$this->validation_error = true;
				}
			}

			// validates an alpha field
			if (array_key_exists("{$key}_alpha", $fields_values)) {
				if (!preg_match('/^[a-zA-Z_]+$/', $value) && strlen(trim($value))) {
					trigger_error($fields_values["{$key}_alpha"]);
					$this->validation_error = true;
				}
			}

			// validates an alphanumeric field
			if (array_key_exists("{$key}_alphanum", $fields_values)) {
				if (!preg_match('/^[a-zA-Z0-9_]+$/', $value) && strlen(trim($value))) {
					trigger_error($fields_values["{$key}_alphanum"]);
					$this->validation_error = true;
				}
			}

			// validates the maximum lenght of the string
			if (array_key_exists("{$key}_maxlen", $fields_values)) {
				$msg = explode(',', $fields_values["{$key}_maxlen"]);
				$maxlen = (int)$msg[0];
				$strlen = strlen($value);
				if ($strlen > $maxlen && strlen(trim($value))) {
					trigger_error($msg[1]);
					$this->validation_error = true;
				}
			}

			// validates the minimum lenght of the string
			if (array_key_exists("{$key}_minlen", $fields_values)) {
				$msg = explode(',', $fields_values["{$key}_minlen"]);
				$minlen = (int)$msg[0];
				$strlen = strlen($value);
				if ($strlen < $minlen && strlen(trim($value))) {
					trigger_error($msg[1]);
					$this->validation_error = true;
				}
			}
		}
	}
	
	function validationError() {
		return (bool)$this->validation_error;
	}
}
?>
Return current item: MySQLConnection