Modules

Valid
extends Kohana_Valid

Validation rules.

package
Kohana
category
Security
author
Kohana Team
copyright
© 2008-2012 Kohana Team
license
http://kohanaframework.org/license

Class declared in SYSPATH/classes/valid.php on line 3.

Methods

public static alpha( string $str [, boolean $utf8 = bool FALSE ] ) (defined in Kohana_Valid)

Checks whether a string consists of alphabetical characters only.

Parameters

  • string $str required - Input string
  • boolean $utf8 = bool FALSE - Trigger UTF-8 compatibility

Return Values

  • boolean

Source Code

public static function alpha($str, $utf8 = FALSE)
{
	$str = (string) $str;

	if ($utf8 === TRUE)
	{
		return (bool) preg_match('/^\pL++$/uD', $str);
	}
	else
	{
		return ctype_alpha($str);
	}
}

public static alpha_dash( string $str [, boolean $utf8 = bool FALSE ] ) (defined in Kohana_Valid)

Checks whether a string consists of alphabetical characters, numbers, underscores and dashes only.

Parameters

  • string $str required - Input string
  • boolean $utf8 = bool FALSE - Trigger UTF-8 compatibility

Return Values

  • boolean

Source Code

public static function alpha_dash($str, $utf8 = FALSE)
{
	if ($utf8 === TRUE)
	{
		$regex = '/^[-\pL\pN_]++$/uD';
	}
	else
	{
		$regex = '/^[-a-z0-9_]++$/iD';
	}

	return (bool) preg_match($regex, $str);
}

public static alpha_numeric( string $str [, boolean $utf8 = bool FALSE ] ) (defined in Kohana_Valid)

Checks whether a string consists of alphabetical characters and numbers only.

Parameters

  • string $str required - Input string
  • boolean $utf8 = bool FALSE - Trigger UTF-8 compatibility

Return Values

  • boolean

Source Code

public static function alpha_numeric($str, $utf8 = FALSE)
{
	if ($utf8 === TRUE)
	{
		return (bool) preg_match('/^[\pL\pN]++$/uD', $str);
	}
	else
	{
		return ctype_alnum($str);
	}
}

public static color( string $str ) (defined in Kohana_Valid)

Checks if a string is a proper hexadecimal HTML color value. The validation is quite flexible as it does not require an initial "#" and also allows for the short notation using only three instead of six hexadecimal characters.

Parameters

  • string $str required - Input string

Return Values

  • boolean

Source Code

public static function color($str)
{
	return (bool) preg_match('/^#?+[0-9a-f]{3}(?:[0-9a-f]{3})?$/iD', $str);
}

public static credit_card( integer $number [, string|array $type = NULL ] ) (defined in Kohana_Valid)

Validates a credit card number, with a Luhn check if possible.

Parameters

  • integer $number required - Credit card number
  • string|array $type = NULL - Card type, or an array of card types

Tags

Return Values

  • boolean

Source Code

public static function credit_card($number, $type = NULL)
{
	// Remove all non-digit characters from the number
	if (($number = preg_replace('/\D+/', '', $number)) === '')
		return FALSE;

	if ($type == NULL)
	{
		// Use the default type
		$type = 'default';
	}
	elseif (is_array($type))
	{
		foreach ($type as $t)
		{
			// Test each type for validity
			if (Valid::credit_card($number, $t))
				return TRUE;
		}

		return FALSE;
	}

	$cards = Kohana::$config->load('credit_cards');

	// Check card type
	$type = strtolower($type);

	if ( ! isset($cards[$type]))
		return FALSE;

	// Check card number length
	$length = strlen($number);

	// Validate the card length by the card type
	if ( ! in_array($length, preg_split('/\D+/', $cards[$type]['length'])))
		return FALSE;

	// Check card number prefix
	if ( ! preg_match('/^'.$cards[$type]['prefix'].'/', $number))
		return FALSE;

	// No Luhn check required
	if ($cards[$type]['luhn'] == FALSE)
		return TRUE;

	return Valid::luhn($number);
}

public static date( string $str ) (defined in Kohana_Valid)

Tests if a string is a valid date string.

Parameters

  • string $str required - Date to check

Return Values

  • boolean

Source Code

public static function date($str)
{
	return (strtotime($str) !== FALSE);
}

public static decimal( string $str [, integer $places = integer 2 , integer $digits = NULL ] ) (defined in Kohana_Valid)

Checks if a string is a proper decimal format. Optionally, a specific number of digits can be checked too.

Parameters

  • string $str required - Number to check
  • integer $places = integer 2 - Number of decimal places
  • integer $digits = NULL - Number of digits

Return Values

  • boolean

Source Code

public static function decimal($str, $places = 2, $digits = NULL)
{
	if ($digits > 0)
	{
		// Specific number of digits
		$digits = '{'.( (int) $digits).'}';
	}
	else
	{
		// Any number of digits
		$digits = '+';
	}

	// Get the decimal point for the current locale
	list($decimal) = array_values(localeconv());

	return (bool) preg_match('/^[+-]?[0-9]'.$digits.preg_quote($decimal).'[0-9]{'.( (int) $places).'}$/D', $str);
}

public static digit( string $str [, boolean $utf8 = bool FALSE ] ) (defined in Kohana_Valid)

Checks whether a string consists of digits only (no dots or dashes).

Parameters

  • string $str required - Input string
  • boolean $utf8 = bool FALSE - Trigger UTF-8 compatibility

Return Values

  • boolean

Source Code

public static function digit($str, $utf8 = FALSE)
{
	if ($utf8 === TRUE)
	{
		return (bool) preg_match('/^\pN++$/uD', $str);
	}
	else
	{
		return (is_int($str) AND $str >= 0) OR ctype_digit($str);
	}
}

public static email( string $email [, boolean $strict = bool FALSE ] ) (defined in Kohana_Valid)

Check an email address for correct format.

Parameters

  • string $email required - Email address
  • boolean $strict = bool FALSE - Strict RFC compatibility

Tags

Return Values

  • boolean

Source Code

public static function email($email, $strict = FALSE)
{
	if (UTF8::strlen($email) > 254)
	{
		return FALSE;
	}

	if ($strict === TRUE)
	{
		$qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
		$dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
		$atom  = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
		$pair  = '\\x5c[\\x00-\\x7f]';

		$domain_literal = "\\x5b($dtext|$pair)*\\x5d";
		$quoted_string  = "\\x22($qtext|$pair)*\\x22";
		$sub_domain     = "($atom|$domain_literal)";
		$word           = "($atom|$quoted_string)";
		$domain         = "$sub_domain(\\x2e$sub_domain)*";
		$local_part     = "$word(\\x2e$word)*";

		$expression     = "/^$local_part\\x40$domain$/D";
	}
	else
	{
		$expression = '/^[-_a-z0-9\'+*$^&%=~!?{}]++(?:\.[-_a-z0-9\'+*$^&%=~!?{}]+)*+@(?:(?![-.])[-a-z0-9.]+(?<![-.])\.[a-z]{2,6}|\d{1,3}(?:\.\d{1,3}){3})$/iD';
	}

	return (bool) preg_match($expression, (string) $email);
}

public static email_domain( string $email ) (defined in Kohana_Valid)

Validate the domain of an email address by checking if the domain has a valid MX record.

Parameters

  • string $email required - Email address

Tags

Return Values

  • boolean

Source Code

public static function email_domain($email)
{
	if ( ! Valid::not_empty($email))
		return FALSE; // Empty fields cause issues with checkdnsrr()

	// Check if the email domain has a valid MX record
	return (bool) checkdnsrr(preg_replace('/^[^@]++@/', '', $email), 'MX');
}

public static equals( string $value , string $required ) (defined in Kohana_Valid)

Checks that a field is exactly the value required.

Parameters

  • string $value required - Value
  • string $required required - Required value

Return Values

  • boolean

Source Code

public static function equals($value, $required)
{
	return ($value === $required);
}

public static exact_length( string $value , integer|array $length ) (defined in Kohana_Valid)

Checks that a field is exactly the right length.

Parameters

  • string $value required - Value
  • integer|array $length required - Exact length required, or array of valid lengths

Return Values

  • boolean

Source Code

public static function exact_length($value, $length)
{
	if (is_array($length))
	{
		foreach($length as $strlen)
		{
			if (UTF8::strlen($value) === $strlen)
				return TRUE;
		}
		return FALSE;
	}

	return UTF8::strlen($value) === $length;
}

public static ip( string $ip [, boolean $allow_private = bool TRUE ] ) (defined in Kohana_Valid)

Validate an IP.

Parameters

  • string $ip required - IP address
  • boolean $allow_private = bool TRUE - Allow private IP networks

Return Values

  • boolean

Source Code

public static function ip($ip, $allow_private = TRUE)
{
	// Do not allow reserved addresses
	$flags = FILTER_FLAG_NO_RES_RANGE;

	if ($allow_private === FALSE)
	{
		// Do not allow private or reserved addresses
		$flags = $flags | FILTER_FLAG_NO_PRIV_RANGE;
	}

	return (bool) filter_var($ip, FILTER_VALIDATE_IP, $flags);
}

public static luhn( string $number ) (defined in Kohana_Valid)

Validate a number against the Luhn (mod10) formula.

Parameters

  • string $number required - Number to check

Return Values

  • boolean

Source Code

public static function luhn($number)
{
	// Force the value to be a string as this method uses string functions.
	// Converting to an integer may pass PHP_INT_MAX and result in an error!
	$number = (string) $number;

	if ( ! ctype_digit($number))
	{
		// Luhn can only be used on numbers!
		return FALSE;
	}

	// Check number length
	$length = strlen($number);

	// Checksum of the card number
	$checksum = 0;

	for ($i = $length - 1; $i >= 0; $i -= 2)
	{
		// Add up every 2nd digit, starting from the right
		$checksum += substr($number, $i, 1);
	}

	for ($i = $length - 2; $i >= 0; $i -= 2)
	{
		// Add up every 2nd digit doubled, starting from the right
		$double = substr($number, $i, 1) * 2;

		// Subtract 9 from the double where value is greater than 10
		$checksum += ($double >= 10) ? ($double - 9) : $double;
	}

	// If the checksum is a multiple of 10, the number is valid
	return ($checksum % 10 === 0);
}

public static matches( array $array , string $field , string $match ) (defined in Kohana_Valid)

Checks if a field matches the value of another field.

Parameters

  • array $array required - Array of values
  • string $field required - Field name
  • string $match required - Field name to match

Return Values

  • boolean

Source Code

public static function matches($array, $field, $match)
{
	return ($array[$field] === $array[$match]);
}

public static max_length( string $value , integer $length ) (defined in Kohana_Valid)

Checks that a field is short enough.

Parameters

  • string $value required - Value
  • integer $length required - Maximum length required

Return Values

  • boolean

Source Code

public static function max_length($value, $length)
{
	return UTF8::strlen($value) <= $length;
}

public static min_length( string $value , integer $length ) (defined in Kohana_Valid)

Checks that a field is long enough.

Parameters

  • string $value required - Value
  • integer $length required - Minimum length required

Return Values

  • boolean

Source Code

public static function min_length($value, $length)
{
	return UTF8::strlen($value) >= $length;
}

public static not_empty( ) (defined in Kohana_Valid)

Checks if a field is not empty.

Return Values

  • boolean

Source Code

public static function not_empty($value)
{
	if (is_object($value) AND $value instanceof ArrayObject)
	{
		// Get the array from the ArrayObject
		$value = $value->getArrayCopy();
	}

	// Value cannot be NULL, FALSE, '', or an empty array
	return ! in_array($value, array(NULL, FALSE, '', array()), TRUE);
}

public static numeric( string $str ) (defined in Kohana_Valid)

Checks whether a string is a valid number (negative and decimal numbers allowed).

Uses {@link http://www.php.net/manual/en/function.localeconv.php locale conversion} to allow decimal point to be locale specific.

Parameters

  • string $str required - Input string

Return Values

  • boolean

Source Code

public static function numeric($str)
{
	// Get the decimal point for the current locale
	list($decimal) = array_values(localeconv());

	// A lookahead is used to make sure the string contains at least one digit (before or after the decimal point)
	return (bool) preg_match('/^-?+(?=.*[0-9])[0-9]*+'.preg_quote($decimal).'?+[0-9]*+$/D', (string) $str);
}

public static phone( string $number [, array $lengths = NULL ] ) (defined in Kohana_Valid)

Checks if a phone number is valid.

Parameters

  • string $number required - Phone number to check
  • array $lengths = NULL - $lengths

Return Values

  • boolean

Source Code

public static function phone($number, $lengths = NULL)
{
	if ( ! is_array($lengths))
	{
		$lengths = array(7,10,11);
	}

	// Remove all non-digit characters from the number
	$number = preg_replace('/\D+/', '', $number);

	// Check if the number is within range
	return in_array(strlen($number), $lengths);
}

public static range( string $number , integer $min , integer $max ) (defined in Kohana_Valid)

Tests if a number is within a range.

Parameters

  • string $number required - Number to check
  • integer $min required - Minimum value
  • integer $max required - Maximum value

Return Values

  • boolean

Source Code

public static function range($number, $min, $max)
{
	return ($number >= $min AND $number <= $max);
}

public static regex( string $value , string $expression ) (defined in Kohana_Valid)

Checks a field against a regular expression.

Parameters

  • string $value required - Value
  • string $expression required - Regular expression to match (including delimiters)

Return Values

  • boolean

Source Code

public static function regex($value, $expression)
{
	return (bool) preg_match($expression, (string) $value);
}

public static url( string $url ) (defined in Kohana_Valid)

Validate a URL.

Parameters

  • string $url required - URL

Return Values

  • boolean

Source Code

public static function url($url)
{
	// Based on http://www.apps.ietf.org/rfc/rfc1738.html#sec-5
	if ( ! preg_match(
		'~^

		# scheme
		[-a-z0-9+.]++://

		# username:password (optional)
		(?:
			    [-a-z0-9$_.+!*\'(),;?&=%]++   # username
			(?::[-a-z0-9$_.+!*\'(),;?&=%]++)? # password (optional)
			@
		)?

		(?:
			# ip address
			\d{1,3}+(?:\.\d{1,3}+){3}+

			| # or

			# hostname (captured)
			(
				     (?!-)[-a-z0-9]{1,63}+(?<!-)
				(?:\.(?!-)[-a-z0-9]{1,63}+(?<!-)){0,126}+
			)
		)

		# port (optional)
		(?::\d{1,5}+)?

		# path (optional)
		(?:/.*)?

		$~iDx', $url, $matches))
		return FALSE;

	// We matched an IP address
	if ( ! isset($matches[1]))
		return TRUE;

	// Check maximum length of the whole hostname
	// http://en.wikipedia.org/wiki/Domain_name#cite_note-0
	if (strlen($matches[1]) > 253)
		return FALSE;

	// An extra check for the top level domain
	// It must start with a letter
	$tld = ltrim(substr($matches[1], (int) strrpos($matches[1], '.')), '.');
	return ctype_alpha($tld[0]);
}