3 * Validation Class. Used for validation of model data
7 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
8 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
10 * Licensed under The MIT License
11 * Redistributions of files must retain the above copyright notice.
13 * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
14 * @link http://cakephp.org CakePHP(tm) Project
16 * @subpackage cake.cake.libs
17 * @since CakePHP(tm) v 1.2.0.3830
18 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
20 if (!class_exists('Multibyte')) {
21 App
::import('Core', 'Multibyte', false);
24 * Offers different validation methods.
27 * @subpackage cake.cake.libs
28 * @since CakePHP v 1.2.0.3830
30 class Validation
extends Object {
33 * Set the value of methods $check param.
41 * Set to a valid regular expression in the class methods.
42 * Can be set from $regex param also
50 * Some complex patterns needed in multiple places
55 var $__pattern = array(
56 'hostname' => '(?:[a-z0-9][-a-z0-9]*\.)*(?:[a-z0-9][-a-z0-9]{0,62})\.(?:(?:[a-z]{2}\.)?[a-z]{2,4}|museum|travel)'
60 * Some class methods use a country to determine proper validation.
61 * This can be passed to methods in the $country param
69 * Some class methods use a deeper validation when set to true
77 * Some class methods use the $type param to determine which validation to perfom in the method
85 * Holds an array of errors messages set in this class.
86 * These are used for debugging purposes
91 var $errors = array();
94 * Gets a reference to the Validation object instance
96 * @return object Validation instance
100 function &getInstance() {
101 static $instance = array();
104 $instance[0] =& new Validation();
110 * Checks that a string contains something other than whitespace
112 * Returns true if string contains something other than whitespace
114 * $check can be passed as an array:
115 * array('check' => 'valueToCheck');
117 * @param mixed $check Value to check
118 * @return boolean Success
121 function notEmpty($check) {
122 $_this =& Validation
::getInstance();
124 $_this->check
= $check;
126 if (is_array($check)) {
127 $_this->_extract($check);
130 if (empty($_this->check
) && $_this->check
!= '0') {
133 $_this->regex
= '/[^\s]+/m';
134 return $_this->_check();
138 * Checks that a string contains only integer or letters
140 * Returns true if string contains only integer or letters
142 * $check can be passed as an array:
143 * array('check' => 'valueToCheck');
145 * @param mixed $check Value to check
146 * @return boolean Success
149 function alphaNumeric($check) {
150 $_this =& Validation
::getInstance();
152 $_this->check
= $check;
154 if (is_array($check)) {
155 $_this->_extract($check);
158 if (empty($_this->check
) && $_this->check
!= '0') {
161 $_this->regex
= '/^[\p{Ll}\p{Lm}\p{Lo}\p{Lt}\p{Lu}\p{Nd}]+$/mu';
162 return $_this->_check();
166 * Checks that a string length is within s specified range.
167 * Spaces are included in the character count.
168 * Returns true is string matches value min, max, or between min and max,
170 * @param string $check Value to check for length
171 * @param integer $min Minimum value in range (inclusive)
172 * @param integer $max Maximum value in range (inclusive)
173 * @return boolean Success
176 function between($check, $min, $max) {
177 $length = mb_strlen($check);
178 return ($length >= $min && $length <= $max);
182 * Returns true if field is left blank -OR- only whitespace characters are present in it's value
183 * Whitespace characters include Space, Tab, Carriage Return, Newline
185 * $check can be passed as an array:
186 * array('check' => 'valueToCheck');
188 * @param mixed $check Value to check
189 * @return boolean Success
192 function blank($check) {
193 $_this =& Validation
::getInstance();
195 $_this->check
= $check;
197 if (is_array($check)) {
198 $_this->_extract($check);
201 $_this->regex
= '/[^\\s]/';
202 return !$_this->_check();
206 * Validation of credit card numbers.
207 * Returns true if $check is in the proper credit card format.
209 * @param mixed $check credit card number to validate
210 * @param mixed $type 'all' may be passed as a sting, defaults to fast which checks format of most major credit cards
211 * if an array is used only the values of the array are checked.
212 * Example: array('amex', 'bankcard', 'maestro')
213 * @param boolean $deep set to true this will check the Luhn algorithm of the credit card.
214 * @param string $regex A custom regex can also be passed, this will be used instead of the defined regex values
215 * @return boolean Success
217 * @see Validation::_luhn()
219 function cc($check, $type = 'fast', $deep = false, $regex = null) {
220 $_this =& Validation
::getInstance();
222 $_this->check
= $check;
223 $_this->type
= $type;
224 $_this->deep
= $deep;
225 $_this->regex
= $regex;
227 if (is_array($check)) {
228 $_this->_extract($check);
230 $_this->check
= str_replace(array('-', ' '), '', $_this->check
);
232 if (mb_strlen($_this->check
) < 13) {
236 if (!is_null($_this->regex
)) {
237 if ($_this->_check()) {
238 return $_this->_luhn();
243 'amex' => '/^3[4|7]\\d{13}$/',
244 'bankcard' => '/^56(10\\d\\d|022[1-5])\\d{10}$/',
245 'diners' => '/^(?:3(0[0-5]|[68]\\d)\\d{11})|(?:5[1-5]\\d{14})$/',
246 'disc' => '/^(?:6011|650\\d)\\d{12}$/',
247 'electron' => '/^(?:417500|4917\\d{2}|4913\\d{2})\\d{10}$/',
248 'enroute' => '/^2(?:014|149)\\d{11}$/',
249 'jcb' => '/^(3\\d{4}|2100|1800)\\d{11}$/',
250 'maestro' => '/^(?:5020|6\\d{3})\\d{12}$/',
251 'mc' => '/^5[1-5]\\d{14}$/',
252 'solo' => '/^(6334[5-9][0-9]|6767[0-9]{2})\\d{10}(\\d{2,3})?$/',
253 'switch' => '/^(?:49(03(0[2-9]|3[5-9])|11(0[1-2]|7[4-9]|8[1-2])|36[0-9]{2})\\d{10}(\\d{2,3})?)|(?:564182\\d{10}(\\d{2,3})?)|(6(3(33[0-4][0-9])|759[0-9]{2})\\d{10}(\\d{2,3})?)$/',
254 'visa' => '/^4\\d{12}(\\d{3})?$/',
255 'voyager' => '/^8699[0-9]{11}$/'
257 'fast' => '/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6011[0-9]{12}|3(?:0[0-5]|[68][0-9])[0-9]{11}|3[47][0-9]{13})$/'
260 if (is_array($_this->type
)) {
261 foreach ($_this->type
as $value) {
262 $_this->regex
= $cards['all'][strtolower($value)];
264 if ($_this->_check()) {
265 return $_this->_luhn();
268 } elseif ($_this->type
== 'all') {
269 foreach ($cards['all'] as $value) {
270 $_this->regex
= $value;
272 if ($_this->_check()) {
273 return $_this->_luhn();
277 $_this->regex
= $cards['fast'];
279 if ($_this->_check()) {
280 return $_this->_luhn();
286 * Used to compare 2 numeric values.
288 * @param mixed $check1 if string is passed for a string must also be passed for $check2
289 * used as an array it must be passed as array('check1' => value, 'operator' => 'value', 'check2' -> value)
290 * @param string $operator Can be either a word or operand
291 * is greater >, is less <, greater or equal >=
292 * less or equal <=, is less <, equal to ==, not equal !=
293 * @param integer $check2 only needed if $check1 is a string
294 * @return boolean Success
297 function comparison($check1, $operator = null, $check2 = null) {
298 if (is_array($check1)) {
299 extract($check1, EXTR_OVERWRITE
);
301 $operator = str_replace(array(' ', "\t", "\n", "\r", "\0", "\x0B"), '', strtolower($operator));
306 if ($check1 > $check2) {
312 if ($check1 < $check2) {
316 case 'greaterorequal':
318 if ($check1 >= $check2) {
324 if ($check1 <= $check2) {
330 if ($check1 == $check2) {
336 if ($check1 != $check2) {
341 $_this =& Validation
::getInstance();
342 $_this->errors
[] = __('You must define the $operator parameter for Validation::comparison()', true);
349 * Used when a custom regular expression is needed.
351 * @param mixed $check When used as a string, $regex must also be a valid regular expression.
352 * As and array: array('check' => value, 'regex' => 'valid regular expression')
353 * @param string $regex If $check is passed as a string, $regex must also be set to valid regular expression
354 * @return boolean Success
357 function custom($check, $regex = null) {
358 $_this =& Validation
::getInstance();
360 $_this->check
= $check;
361 $_this->regex
= $regex;
362 if (is_array($check)) {
363 $_this->_extract($check);
365 if ($_this->regex
=== null) {
366 $_this->errors
[] = __('You must define a regular expression for Validation::custom()', true);
369 return $_this->_check();
373 * Date validation, determines if the string passed is a valid date.
374 * keys that expect full month, day and year will validate leap years
376 * @param string $check a valid date string
377 * @param mixed $format Use a string or an array of the keys below. Arrays should be passed as array('dmy', 'mdy', etc)
378 * Keys: dmy 27-12-2006 or 27-12-06 separators can be a space, period, dash, forward slash
379 * mdy 12-27-2006 or 12-27-06 separators can be a space, period, dash, forward slash
380 * ymd 2006-12-27 or 06-12-27 separators can be a space, period, dash, forward slash
381 * dMy 27 December 2006 or 27 Dec 2006
382 * Mdy December 27, 2006 or Dec 27, 2006 comma is optional
383 * My December 2006 or Dec 2006
384 * my 12/2006 separators can be a space, period, dash, forward slash
385 * @param string $regex If a custom regular expression is used this is the only validation that will occur.
386 * @return boolean Success
389 function date($check, $format = 'ymd', $regex = null) {
390 $_this =& Validation
::getInstance();
392 $_this->check
= $check;
393 $_this->regex
= $regex;
395 if (!is_null($_this->regex
)) {
396 return $_this->_check();
399 $regex['dmy'] = '%^(?:(?:31(\\/|-|\\.|\\x20)(?:0?[13578]|1[02]))\\1|(?:(?:29|30)(\\/|-|\\.|\\x20)(?:0?[1,3-9]|1[0-2])\\2))(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$|^(?:29(\\/|-|\\.|\\x20)0?2\\3(?:(?:(?:1[6-9]|[2-9]\\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\\d|2[0-8])(\\/|-|\\.|\\x20)(?:(?:0?[1-9])|(?:1[0-2]))\\4(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$%';
400 $regex['mdy'] = '%^(?:(?:(?:0?[13578]|1[02])(\\/|-|\\.|\\x20)31)\\1|(?:(?:0?[13-9]|1[0-2])(\\/|-|\\.|\\x20)(?:29|30)\\2))(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$|^(?:0?2(\\/|-|\\.|\\x20)29\\3(?:(?:(?:1[6-9]|[2-9]\\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:(?:0?[1-9])|(?:1[0-2]))(\\/|-|\\.|\\x20)(?:0?[1-9]|1\\d|2[0-8])\\4(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$%';
401 $regex['ymd'] = '%^(?:(?:(?:(?:(?:1[6-9]|[2-9]\\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(\\/|-|\\.|\\x20)(?:0?2\\1(?:29)))|(?:(?:(?:1[6-9]|[2-9]\\d)?\\d{2})(\\/|-|\\.|\\x20)(?:(?:(?:0?[13578]|1[02])\\2(?:31))|(?:(?:0?[1,3-9]|1[0-2])\\2(29|30))|(?:(?:0?[1-9])|(?:1[0-2]))\\2(?:0?[1-9]|1\\d|2[0-8]))))$%';
402 $regex['dMy'] = '/^((31(?!\\ (Feb(ruary)?|Apr(il)?|June?|(Sep(?=\\b|t)t?|Nov)(ember)?)))|((30|29)(?!\\ Feb(ruary)?))|(29(?=\\ Feb(ruary)?\\ (((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)))))|(0?[1-9])|1\\d|2[0-8])\\ (Jan(uary)?|Feb(ruary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|Aug(ust)?|Oct(ober)?|(Sep(?=\\b|t)t?|Nov|Dec)(ember)?)\\ ((1[6-9]|[2-9]\\d)\\d{2})$/';
403 $regex['Mdy'] = '/^(?:(((Jan(uary)?|Ma(r(ch)?|y)|Jul(y)?|Aug(ust)?|Oct(ober)?|Dec(ember)?)\\ 31)|((Jan(uary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|Aug(ust)?|Oct(ober)?|(Sept|Nov|Dec)(ember)?)\\ (0?[1-9]|([12]\\d)|30))|(Feb(ruary)?\\ (0?[1-9]|1\\d|2[0-8]|(29(?=,?\\ ((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)))))))\\,?\\ ((1[6-9]|[2-9]\\d)\\d{2}))$/';
404 $regex['My'] = '%^(Jan(uary)?|Feb(ruary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|Aug(ust)?|Oct(ober)?|(Sep(?=\\b|t)t?|Nov|Dec)(ember)?)[ /]((1[6-9]|[2-9]\\d)\\d{2})$%';
405 $regex['my'] = '%^(((0[123456789]|10|11|12)([- /.])(([1][9][0-9][0-9])|([2][0-9][0-9][0-9]))))$%';
407 $format = (is_array($format)) ?
array_values($format) : array($format);
408 foreach ($format as $key) {
409 $_this->regex
= $regex[$key];
411 if ($_this->_check() === true) {
419 * Time validation, determines if the string passed is a valid time.
420 * Validates time as 24hr (HH:MM) or am/pm ([H]H:MM[a|p]m)
421 * Does not allow/validate seconds.
423 * @param string $check a valid time string
424 * @return boolean Success
428 function time($check) {
429 $_this =& Validation
::getInstance();
431 $_this->check
= $check;
432 $_this->regex
= '%^((0?[1-9]|1[012])(:[0-5]\d){0,2}([AP]M|[ap]m))$|^([01]\d|2[0-3])(:[0-5]\d){0,2}$%';
433 return $_this->_check();
437 * Boolean validation, determines if value passed is a boolean integer or true/false.
439 * @param string $check a valid boolean
440 * @return boolean Success
443 function boolean($check) {
444 $booleanList = array(0, 1, '0', '1', true, false);
445 return in_array($check, $booleanList, true);
449 * Checks that a value is a valid decimal. If $places is null, the $check is allowed to be a scientific float
450 * If no decimal point is found a false will be returned. Both the sign and exponent are optional.
452 * @param integer $check The value the test for decimal
453 * @param integer $places if set $check value must have exactly $places after the decimal point
454 * @param string $regex If a custom regular expression is used this is the only validation that will occur.
455 * @return boolean Success
458 function decimal($check, $places = null, $regex = null) {
459 $_this =& Validation
::getInstance();
461 $_this->regex
= $regex;
462 $_this->check
= $check;
464 if (is_null($_this->regex
)) {
465 if (is_null($places)) {
466 $_this->regex
= '/^[-+]?[0-9]*\\.{1}[0-9]+(?:[eE][-+]?[0-9]+)?$/';
468 $_this->regex
= '/^[-+]?[0-9]*\\.{1}[0-9]{'.$places.'}$/';
471 return $_this->_check();
475 * Validates for an email address.
477 * @param string $check Value to check
478 * @param boolean $deep Perform a deeper validation (if true), by also checking availability of host
479 * @param string $regex Regex to use (if none it will use built in regex)
480 * @return boolean Success
483 function email($check, $deep = false, $regex = null) {
484 $_this =& Validation
::getInstance();
486 $_this->check
= $check;
487 $_this->regex
= $regex;
488 $_this->deep
= $deep;
490 if (is_array($check)) {
491 $_this->_extract($check);
494 if (is_null($_this->regex
)) {
495 $_this->regex
= '/^[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+)*@' . $_this->__pattern
['hostname'] . '$/i';
497 $return = $_this->_check();
499 if ($_this->deep
=== false ||
$_this->deep
=== null) {
503 if ($return === true && preg_match('/@(' . $_this->__pattern
['hostname'] . ')$/i', $_this->check
, $regs)) {
504 if (function_exists('getmxrr') && getmxrr($regs[1], $mxhosts)) {
507 if (function_exists('checkdnsrr') && checkdnsrr($regs[1], 'MX')) {
510 return is_array(gethostbynamel($regs[1]));
516 * Check that value is exactly $comparedTo.
518 * @param mixed $check Value to check
519 * @param mixed $comparedTo Value to compare
520 * @return boolean Success
523 function equalTo($check, $comparedTo) {
524 return ($check === $comparedTo);
528 * Check that value has a valid file extension.
530 * @param mixed $check Value to check
531 * @param array $extensions file extenstions to allow
532 * @return boolean Success
535 function extension($check, $extensions = array('gif', 'jpeg', 'png', 'jpg')) {
536 if (is_array($check)) {
537 return Validation
::extension(array_shift($check), $extensions);
539 $extension = strtolower(array_pop(explode('.', $check)));
540 foreach ($extensions as $value) {
541 if ($extension == strtolower($value)) {
549 * Validation of an IP address.
551 * Valid IP version strings for type restriction are:
552 * - both: Check both IPv4 and IPv6, return true if the supplied address matches either version
553 * - IPv4: Version 4 (Eg: 127.0.0.1, 192.168.10.123, 203.211.24.8)
554 * - IPv6: Version 6 (Eg: ::1, 2001:0db8::1428:57ab)
556 * @param string $check The string to test.
557 * @param string $type The IP Version to test against
558 * @return boolean Success
561 function ip($check, $type = 'both') {
562 $_this =& Validation
::getInstance();
564 $type = strtolower($type);
565 if ($type === 'ipv4' ||
$type === 'both') {
566 $success |
= $_this->_ipv4($check);
568 if ($type === 'ipv6' ||
$type === 'both') {
569 $success |
= $_this->_ipv6($check);
575 * Validation of IPv4 addresses.
577 * @param string $check IP Address to test
578 * @return boolean Success
581 function _ipv4($check) {
582 if (function_exists('filter_var')) {
583 return filter_var($check, FILTER_VALIDATE_IP
, array('flags' => FILTER_FLAG_IPV4
)) !== false;
585 $this->__populateIp();
586 $this->check
= $check;
587 $this->regex
= '/^' . $this->__pattern
['IPv4'] . '$/';
588 return $this->_check();
592 * Validation of IPv6 addresses.
594 * @param string $check IP Address to test
595 * @return boolean Success
598 function _ipv6($check) {
599 if (function_exists('filter_var')) {
600 return filter_var($check, FILTER_VALIDATE_IP
, array('flags' => FILTER_FLAG_IPV6
)) !== false;
602 $this->__populateIp();
603 $this->check
= $check;
604 $this->regex
= '/^' . $this->__pattern
['IPv6'] . '$/';
605 return $this->_check();
609 * Checks whether the length of a string is greater or equal to a minimal length.
611 * @param string $check The string to test
612 * @param integer $min The minimal string length
613 * @return boolean Success
616 function minLength($check, $min) {
617 $length = mb_strlen($check);
618 return ($length >= $min);
622 * Checks whether the length of a string is smaller or equal to a maximal length..
624 * @param string $check The string to test
625 * @param integer $max The maximal string length
626 * @return boolean Success
629 function maxLength($check, $max) {
630 $length = mb_strlen($check);
631 return ($length <= $max);
635 * Checks that a value is a monetary amount.
637 * @param string $check Value to check
638 * @param string $symbolPosition Where symbol is located (left/right)
639 * @return boolean Success
642 function money($check, $symbolPosition = 'left') {
643 $_this =& Validation
::getInstance();
644 $_this->check
= $check;
646 if ($symbolPosition == 'right') {
647 $_this->regex
= '/^(?!0,?\d)(?:\d{1,3}(?:([, .])\d{3})?(?:\1\d{3})*|(?:\d+))((?!\1)[,.]\d{2})?(?<!\x{00a2})\p{Sc}?$/u';
649 $_this->regex
= '/^(?!\x{00a2})\p{Sc}?(?!0,?\d)(?:\d{1,3}(?:([, .])\d{3})?(?:\1\d{3})*|(?:\d+))((?!\1)[,.]\d{2})?$/u';
651 return $_this->_check();
655 * Validate a multiple select.
659 * - in => provide a list of choices that selections must be made from
660 * - max => maximun number of non-zero choices that can be made
661 * - min => minimum number of non-zero choices that can be made
663 * @param mixed $check Value to check
664 * @param mixed $options Options for the check.
665 * @return boolean Success
668 function multiple($check, $options = array()) {
669 $defaults = array('in' => null, 'max' => null, 'min' => null);
670 $options = array_merge($defaults, $options);
671 $check = array_filter((array)$check);
675 if ($options['max'] && count($check) > $options['max']) {
678 if ($options['min'] && count($check) < $options['min']) {
681 if ($options['in'] && is_array($options['in'])) {
682 foreach ($check as $val) {
683 if (!in_array($val, $options['in'])) {
692 * Checks if a value is numeric.
694 * @param string $check Value to check
695 * @return boolean Succcess
698 function numeric($check) {
699 return is_numeric($check);
703 * Check that a value is a valid phone number.
705 * @param mixed $check Value to check (string or array)
706 * @param string $regex Regular expression to use
707 * @param string $country Country code (defaults to 'all')
708 * @return boolean Success
711 function phone($check, $regex = null, $country = 'all') {
712 $_this =& Validation
::getInstance();
713 $_this->check
= $check;
714 $_this->regex
= $regex;
715 $_this->country
= $country;
716 if (is_array($check)) {
717 $_this->_extract($check);
720 if (is_null($_this->regex
)) {
721 switch ($_this->country
) {
725 // includes all NANPA members. see http://en.wikipedia.org/wiki/North_American_Numbering_Plan#List_of_NANPA_countries_and_territories
726 $_this->regex
= '/^(?:\+?1)?[-. ]?\\(?[2-9][0-8][0-9]\\)?[-. ]?[2-9][0-9]{2}[-. ]?[0-9]{4}$/';
730 if (empty($_this->regex
)) {
731 return $_this->_pass('phone', $check, $country);
733 return $_this->_check();
737 * Checks that a given value is a valid postal code.
739 * @param mixed $check Value to check
740 * @param string $regex Regular expression to use
741 * @param string $country Country to use for formatting
742 * @return boolean Success
745 function postal($check, $regex = null, $country = null) {
746 $_this =& Validation
::getInstance();
747 $_this->check
= $check;
748 $_this->regex
= $regex;
749 $_this->country
= $country;
750 if (is_array($check)) {
751 $_this->_extract($check);
753 if (empty($country)) {
754 $_this->country
= 'us';
757 if (is_null($_this->regex
)) {
758 switch ($_this->country
) {
760 $_this->regex
= '/\\A\\b[A-Z]{1,2}[0-9][A-Z0-9]? [0-9][ABD-HJLNP-UW-Z]{2}\\b\\z/i';
763 $_this->regex
= '/\\A\\b[ABCEGHJKLMNPRSTVXY][0-9][A-Z] ?[0-9][A-Z][0-9]\\b\\z/i';
767 $_this->regex
= '/^[0-9]{5}$/i';
770 $_this->regex
= '/^[1-9]{1}[0-9]{3}$/i';
773 $_this->regex
= '/\\A\\b[0-9]{5}(?:-[0-9]{4})?\\b\\z/i';
777 if (empty($_this->regex
)) {
778 return $_this->_pass('postal', $check, $country);
780 return $_this->_check();
784 * Validate that a number is in specified range.
785 * if $lower and $upper are not set, will return true if
786 * $check is a legal finite on this platform
788 * @param string $check Value to check
789 * @param integer $lower Lower limit
790 * @param integer $upper Upper limit
791 * @return boolean Success
794 function range($check, $lower = null, $upper = null) {
795 if (!is_numeric($check)) {
798 if (isset($lower) && isset($upper)) {
799 return ($check > $lower && $check < $upper);
801 return is_finite($check);
805 * Checks that a value is a valid Social Security Number.
807 * @param mixed $check Value to check
808 * @param string $regex Regular expression to use
809 * @param string $country Country
810 * @return boolean Success
813 function ssn($check, $regex = null, $country = null) {
814 $_this =& Validation
::getInstance();
815 $_this->check
= $check;
816 $_this->regex
= $regex;
817 $_this->country
= $country;
818 if (is_array($check)) {
819 $_this->_extract($check);
822 if (is_null($_this->regex
)) {
823 switch ($_this->country
) {
825 $_this->regex
= '/\\A\\b[0-9]{6}-[0-9]{4}\\b\\z/i';
828 $_this->regex
= '/\\A\\b[0-9]{9}\\b\\z/i';
831 $_this->regex
= '/\\A\\b[0-9]{3}-[0-9]{2}-[0-9]{4}\\b\\z/i';
835 if (empty($_this->regex
)) {
836 return $_this->_pass('ssn', $check, $country);
838 return $_this->_check();
842 * Checks that a value is a valid uuid - http://tools.ietf.org/html/rfc4122
844 * @param string $check Value to check
845 * @return boolean Success
848 function uuid($check) {
849 $_this =& Validation
::getInstance();
850 $_this->check
= $check;
851 $_this->regex
= '/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i';
852 return $_this->_check();
856 * Checks that a value is a valid URL according to http://www.w3.org/Addressing/URL/url-spec.txt
858 * The regex checks for the following component parts:
860 * - a valid, optional, scheme
861 * - a valid ip address OR
862 * a valid domain name as defined by section 2.3.1 of http://www.ietf.org/rfc/rfc1035.txt
863 * with an optional port number
864 * - an optional valid path
865 * - an optional query string (get parameters)
866 * - an optional fragment (anchor tag)
868 * @param string $check Value to check
869 * @param boolean $strict Require URL to be prefixed by a valid scheme (one of http(s)/ftp(s)/file/news/gopher)
870 * @return boolean Success
873 function url($check, $strict = false) {
874 $_this =& Validation
::getInstance();
875 $_this->__populateIp();
876 $_this->check
= $check;
877 $validChars = '([' . preg_quote('!"$&\'()*+,-.@_:;=~[]') . '\/0-9a-z\p{L}\p{N}]|(%[0-9a-f]{2}))';
878 $_this->regex
= '/^(?:(?:https?|ftps?|file|news|gopher):\/\/)' . (!empty($strict) ?
'' : '?') .
879 '(?:' . $_this->__pattern
['IPv4'] . '|\[' . $_this->__pattern
['IPv6'] . '\]|' . $_this->__pattern
['hostname'] . ')' .
880 '(?::[1-9][0-9]{0,4})?' .
881 '(?:\/?|\/' . $validChars . '*)?' .
882 '(?:\?' . $validChars . '*)?' .
883 '(?:#' . $validChars . '*)?$/iu';
884 return $_this->_check();
888 * Checks if a value is in a given list.
890 * @param string $check Value to check
891 * @param array $list List to check against
892 * @return boolean Succcess
895 function inList($check, $list) {
896 return in_array($check, $list);
900 * Runs an user-defined validation.
902 * @param mixed $check value that will be validated in user-defined methods.
903 * @param object $object class that holds validation method
904 * @param string $method class method name for validation to run
905 * @param array $args arguments to send to method
906 * @return mixed user-defined class class method returns
909 function userDefined($check, $object, $method, $args = null) {
910 return call_user_func_array(array(&$object, $method), array($check, $args));
914 * Attempts to pass unhandled Validation locales to a class starting with $classPrefix
915 * and ending with Validation. For example $classPrefix = 'nl', the class would be
918 * @param string $method The method to call on the other class.
919 * @param mixed $check The value to check or an array of parameters for the method to be called.
920 * @param string $classPrefix The prefix for the class to do the validation.
921 * @return mixed Return of Passed method, false on failure
924 function _pass($method, $check, $classPrefix) {
925 $className = ucwords($classPrefix) . 'Validation';
926 if (!class_exists($className)) {
927 trigger_error(sprintf(__('Could not find %s class, unable to complete validation.', true), $className), E_USER_WARNING
);
930 if (!is_callable(array($className, $method))) {
931 trigger_error(sprintf(__('Method %s does not exist on %s unable to complete validation.', true), $method, $className), E_USER_WARNING
);
934 $check = (array)$check;
935 return call_user_func_array(array($className, $method), $check);
939 * Runs a regular expression match.
941 * @return boolean Success of match
945 $_this =& Validation
::getInstance();
946 if (preg_match($_this->regex
, $_this->check
)) {
947 $_this->error
[] = false;
950 $_this->error
[] = true;
956 * Get the values to use when value sent to validation method is
959 * @param array $params Parameters sent to validation method
963 function _extract($params) {
964 $_this =& Validation
::getInstance();
965 extract($params, EXTR_OVERWRITE
);
968 $_this->check
= $check;
971 $_this->regex
= $regex;
973 if (isset($country)) {
974 $_this->country
= mb_strtolower($country);
977 $_this->deep
= $deep;
980 $_this->type
= $type;
987 * @see http://en.wikipedia.org/wiki/Luhn_algorithm
988 * @return boolean Success
992 $_this =& Validation
::getInstance();
993 if ($_this->deep
!== true) {
996 if ($_this->check
== 0) {
1000 $length = strlen($_this->check
);
1002 for ($position = 1 - ($length %
2); $position < $length; $position +
= 2) {
1003 $sum +
= $_this->check
[$position];
1006 for ($position = ($length %
2); $position < $length; $position +
= 2) {
1007 $number = $_this->check
[$position] * 2;
1008 $sum +
= ($number < 10) ?
$number : $number - 9;
1011 return ($sum %
10 == 0);
1015 * Lazily popualate the IP address patterns used for validations
1020 function __populateIp() {
1021 if (!isset($this->__pattern
['IPv6'])) {
1022 $pattern = '((([0-9A-Fa-f]{1,4}:){7}(([0-9A-Fa-f]{1,4})|:))|(([0-9A-Fa-f]{1,4}:){6}';
1023 $pattern .= '(:|((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})';
1024 $pattern .= '|(:[0-9A-Fa-f]{1,4})))|(([0-9A-Fa-f]{1,4}:){5}((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})';
1025 $pattern .= '(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(([0-9A-Fa-f]{1,4}:)';
1026 $pattern .= '{4}(:[0-9A-Fa-f]{1,4}){0,1}((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2}))';
1027 $pattern .= '{3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(([0-9A-Fa-f]{1,4}:){3}(:[0-9A-Fa-f]{1,4}){0,2}';
1028 $pattern .= '((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|';
1029 $pattern .= '((:[0-9A-Fa-f]{1,4}){1,2})))|(([0-9A-Fa-f]{1,4}:){2}(:[0-9A-Fa-f]{1,4}){0,3}';
1030 $pattern .= '((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2}))';
1031 $pattern .= '{3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(([0-9A-Fa-f]{1,4}:)(:[0-9A-Fa-f]{1,4})';
1032 $pattern .= '{0,4}((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)';
1033 $pattern .= '|((:[0-9A-Fa-f]{1,4}){1,2})))|(:(:[0-9A-Fa-f]{1,4}){0,5}((:((25[0-5]|2[0-4]';
1034 $pattern .= '\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4})';
1035 $pattern .= '{1,2})))|(((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})))(%.+)?';
1037 $this->__pattern
['IPv6'] = $pattern;
1039 if (!isset($this->__pattern
['IPv4'])) {
1040 $pattern = '(?:(?:25[0-5]|2[0-4][0-9]|(?:(?:1[0-9])?|[1-9]?)[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|(?:(?:1[0-9])?|[1-9]?)[0-9])';
1041 $this->__pattern
['IPv4'] = $pattern;
1046 * Reset internal variables for another validation run.
1051 function __reset() {
1052 $this->check
= null;
1053 $this->regex
= null;
1054 $this->country
= null;
1057 $this->error
= array();
1058 $this->errors
= array();