Last active
April 23, 2019 17:35
-
-
Save miguelfrmn/3839057 to your computer and use it in GitHub Desktop.
Spanish Validations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Spanish information validator | |
*/ | |
class Validator { | |
static function validarDni($cif) { | |
//Copyright ©2005-2008 David Vidal Serra. Bajo licencia GNU GPL. | |
//Este software viene SIN NINGUN TIPO DE GARANTIA; para saber mas detalles | |
//puede consultar la licencia en http://www.gnu.org/licenses/gpl.txt(1) | |
//Esto es software libre, y puede ser usado y redistribuirdo de acuerdo | |
//con la condicion de que el autor jamas sera responsable de su uso. | |
//Returns: 1 = NIF ok, 2 = CIF ok, 3 = NIE ok, -1 = NIF bad, -2 = CIF bad, -3 = NIE bad, 0 = ??? bad | |
$cif = strtoupper($cif); | |
for ($i = 0; $i < 9; $i ++) | |
$num[$i] = substr($cif, $i, 1); | |
//si no tiene un formato valido devuelve error | |
if (!@ereg('((^[A-Z]{1}[0-9]{7}[A-Z0-9]{1}$|^[T]{1}[A-Z0-9]{8}$)|^[0-9]{8}[A-Z]{1}$)', $cif)) | |
return 0; | |
//comprobacion de NIFs estandar | |
if (@ereg('(^[0-9]{8}[A-Z]{1}$)', $cif)) | |
if ($num[8] == substr('TRWAGMYFPDXBNJZSQVHLCKE', substr($cif, 0, 8) % 23, 1)) | |
return 1; | |
else | |
return -1; | |
//algoritmo para comprobacion de codigos tipo CIF | |
$suma = $num[2] + $num[4] + $num[6]; | |
for ($i = 1; $i < 8; $i += 2) | |
$suma += substr((2 * $num[$i]),0,1) + substr((2 * $num[$i]),1,1); | |
$n = 10 - substr($suma, strlen($suma) - 1, 1); | |
//comprobacion de NIFs especiales (se calculan como CIFs) | |
if (@ereg('^[KLM]{1}', $cif)) | |
if ($num[8] == chr(64 + $n)) | |
return 1; | |
else | |
return -1; | |
//comprobacion de CIFs | |
if (@ereg('^[ABCDEFGHJNPQRSUVW]{1}', $cif)) | |
if ($num[8] == chr(64 + $n) || $num[8] == substr($n, strlen($n) - 1, 1)) | |
return 2; | |
else | |
return -2; | |
//comprobacion de NIEs | |
//T | |
if (@ereg('^[T]{1}', $cif)) | |
if ($num[8] == @ereg('^[T]{1}[A-Z0-9]{8}$', $cif)) | |
return 3; | |
else | |
return -3; | |
//XYZ | |
if (@ereg('^[XYZ]{1}', $cif)) | |
if ($num[8] == substr('TRWAGMYFPDXBNJZSQVHLCKE', substr(str_replace(array('X','Y','Z'), array('0','1','2'), $cif), 0, 8) % 23, 1)) | |
return 3; | |
else | |
return -3; | |
//si todavia no se ha verificado devuelve error | |
return 0; | |
} | |
static function validarBI($bi) { | |
if (preg_match('/^[0-9]{8,8}$/', $bi)) { | |
return true; | |
} | |
return false; | |
} | |
static function validarPostal($codigo){ | |
if (preg_match('/^[0-9]{5,5}$/', $codigo)) { | |
$codigo = (int)$codigo; | |
if($codigo >= 52999){ | |
return false; | |
} | |
return true; | |
} | |
return false; | |
} | |
static function validarPostalPortugal($codigo){ | |
if (preg_match('/^[0-9]{4}-[0-9]{3}$/', $codigo)) { | |
return true; | |
} | |
return false; | |
} | |
static function validarEmail($email){ | |
return filter_var($email, FILTER_VALIDATE_EMAIL); | |
} | |
static function validarTelefono($tel, $rule = 'all'){ | |
if(empty($tel)){ return false; } | |
if(strlen($tel) != 9) { return false; } | |
$regex = '/^[6789][0-9]{8,8}$/i'; | |
if($rule == 'mobile'){ | |
$regex = '/^[67][0-9]{8,8}$/i'; | |
} else if($rule == 'landline'){ | |
$regex = '/^9[0-9]{8,8}$/i'; | |
} | |
if(!preg_match($regex, $tel)){ | |
return false; | |
} | |
return true; | |
} | |
static function validateFileSize($fileArray, $maxLength) { | |
return $fileArray['size'] <= $maxLength; | |
} | |
static function validateFileExtension($fileArray, $allowedExtensions) { | |
$ext = strtolower(end(explode('.', $fileArray['name']))); | |
if (!in_array($ext, $allowedExtensions)) { | |
return false; | |
} | |
return true; | |
} | |
/** | |
* Validate a date in many formats | |
* @param mixed $date The date to validate. | |
* It can be a DateTime, returns itself | |
* It can be a string with formats: YYYY-MM-DD, DD/MM/YYYY | |
* It can be an array with formats: array('year' => xx, 'month' => xx, 'day' => xx) | |
* @return mixed a Datetime object if correct, false if incorrect | |
*/ | |
static function validateDate($date) { | |
if (gettype($date) == 'object' && get_class($date) === 'DateTime') { | |
return $date; | |
} | |
if (is_string($date) && preg_match('/\d{1,2}\/\d{1,2}\/\d{4}/i', $date)) { | |
$date = trim($date); | |
list($day, $month, $year) = explode('/', $date); | |
} else if (is_string($date) && preg_match('/\d{4}\-\d{1,2}\-\d{1,2}/i', $date)) { | |
$date = trim($date); | |
list($year, $month, $day) = explode('-', $date); | |
} else if (is_array($date)) { | |
$year = @$date['year']; | |
$month = @$date['month']; | |
$day = @$date['day']; | |
} else { | |
return false; | |
} | |
if (!checkdate((int)$month, (int)$day, (int)$year)) { | |
return false; | |
} | |
$obj = new \DateTime(); | |
$obj->setDate((int)$year, (int)$month, (int)$day); | |
$obj->setTime(0, 0); | |
return $obj; | |
} | |
static function validateAge($birthday, $minAge = 18, $curDate = null) { | |
$birthday = self::validateDate($birthday); | |
if ($birthday === false) { | |
return false; | |
} | |
if (!$curDate) { | |
$curDate = new DateTime(); | |
} | |
$interval = $birthday->diff($curDate); | |
if ($minAge > $interval->y) { | |
return false; | |
} | |
return true; | |
} | |
} | |
if (false) { // Poor man tests | |
ini_set('assert.exception', 1); | |
$res = Validator::validateDate('2015-02-10'); | |
assert(is_object($res)); | |
$res = Validator::validateDate('1/1/2000'); | |
assert(is_object($res)); | |
$res = Validator::validateDate('1/01/2000'); | |
assert(is_object($res)); | |
$res = Validator::validateDate(array('year' => 2000, 'month' => 2, 'day' => 23)); | |
assert(is_object($res)); | |
$res = Validator::validateDate('31/02/2000'); | |
assert($res === false); | |
$res = Validator::validateDate('2015-02-30'); | |
assert($res === false); | |
$res = Validator::validateDate('2015---1-30'); | |
assert($res === false); | |
$res = Validator::validateDate(array('year' => 2000, 'month' => null, 'day' => 23)); | |
assert($res === false); | |
$res = Validator::validateDate(array('year' => 2000, 'month' => 2)); | |
assert($res === false); | |
$res = Validator::validateDate(array('year' => 2000, 'month' => 2, 'day' => 31)); | |
assert($res === false); | |
$res = Validator::validateAge(new DateTime('2015-05-24'), 18, new DateTime('2016-05-24')); // 1 year old, not older than 18 | |
assert($res === false); | |
$res = Validator::validateAge(new DateTime('2000-05-24'), 18, new DateTime('2016-05-24')); // 16 years old, not older than 18 | |
assert($res === false); | |
$res = Validator::validateAge(new DateTime('1998-05-25'), 18, new DateTime('2016-05-24')); // 17.99 years old, not older than 18 | |
assert($res === false); | |
$res = Validator::validateAge(new DateTime('1998-05-24'), 18, new DateTime('2016-05-24')); // 18 years old, older than 18 | |
assert($res === true); | |
$res = Validator::validateAge(new DateTime('1998-05-23'), 18, new DateTime('2016-05-24')); // 18.1 years old, older than 18 | |
assert($res === true); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment