Código PHP:
private function validateINTEGER($value)
{
$return="El campo $this->field ";
if(!ctype_digit(strval($value)))
return $this->showValidation($return."ha de ser un número entero");
$options=array("options"=>array());
if(isset($this->min))
$options["options"]["min_range"]=$this->min;
if(isset($this->max))
$options["options"]["max_range"]=$this->max;
if(!filter_var($value, FILTER_VALIDATE_INT,$options)) {
if(isset($this->min) && isset($this->max))
$return.="ha de estar en el rango [$this->min - $this->max]";
elseif(isset($this->min) && !isset($this->max))
$return.="ha de ser mayor que $this->min";
elseif(!isset($this->min) && isset($this->max))
$return.="ha de ser menor que $this->max";
return $this->showValidation($return);
}
return $this->showValidation();
}
private function validateFLOAT($value)
{
$return="El campo $this->field ";
if(!is_numeric(strval($value)))
return $this->showValidation($return."ha de ser un numérico");
if($this->numdec==0)
return $this->validateINTEGER($value);
if(!filter_var($value, FILTER_VALIDATE_FLOAT))
return $this->showValidation($return."no es un numérico válido");
if(strpos($value,".")!==false) {
$qdecimals=explode(".",$value);
$qdecimals=strlen($qdecimals[1]);
if($qdecimals!=$this->numdec)
$this->showValidation($return."ha de tener $this->numdec decimales");
elseif(isset($this->min) || isset($this->max)) {
if(isset($this->min) && !isset($this->max)) {
if($value<$this->min)
return $this->showValidation($return."ha de ser mayor que $this->min");
}
elseif(!isset($this->min) && isset($this->max)) {
if($value>$this->max)
return $this->showValidation($return."ha de ser menor que $this->max");
}
else {
if($value>$this->max || $value<$this->min) {
if($value>$this->max && $value>=$this->min)
return $this->showValidation($return."ha de ser menor que $this->max");
elseif($value<=$this->max && $value<$this->min)
return $this->showValidation($return."ha de ser mayor que $this->min");
else
return $this->showValidation($return."ha de estar en el rango [$this->min - $this->max]");
}
}
}
}
else
return $this->showValidation($return."ha de tener $this->numdec decimales");
return $this->showValidation();
}
private function extractDate($date)
{
$date=explode($this->sep,$date);
$month=$date[1];
if($this->sql==0) {
$year=$date[2];
$day=$date[0];
}
else {
$year=$date[0];
$day=$date[2];
}
if($day<10) $day="0".$day;
if($month<10) $day="0".$month;
return $year.$month.$day;
}
private function validateDATE($value)
{
$return="El campo $this->field ";
$date=explode($this->sep,$value);
if(count($date)!=3 || strlen($value)!=10) {
if($this->sql==0)
$format="dd".$this->sep."mm".$this->sep."yyyy";
else
$format="yyyy".$this->sep."mm".$this->sep."dd";
return $this->showValidation($return."no tiene el formato correcto ($format)");
}
$month=$date[1];
if($this->sql==0) {
$year=$date[2];
$day=$date[0];
}
else {
$year=$date[0];
$day=$date[2];
}
if(!checkdate((int)$month,(int)$day,(int)$year))
return $this->showValidation($return."no es correcto (fecha inválida)");
if(isset($this->mindate) && isset($this->maxdate)) {
$mindate=$this->extractDate($this->mindate);
$maxdate=$this->extractDate($this->maxdate);
$date=$this->extractDate($value);
if($date>$maxdate || $date<$mindate)
return $this->showValidation($return."ha de estar en el rango [$this->mindate , $this->maxdate]");
}
elseif(isset($this->mindate) && !isset($this->maxdate)) {
$mindate=$this->extractDate($this->mindate);
$date=$this->extractDate($value);
if($date<$mindate)
return $this->showValidation($return."ha de ser mayor que $this->mindate");
}
elseif(!isset($this->mindate) && isset($this->maxdate)) {
$maxdate=$this->extractDate($this->maxdate);
$date=$this->extractDate($value);
if($date>$maxdate)
return $this->showValidation($return."ha de ser menor que $this->maxdate");
}
return $this->showValidation();
}
private function DNI_Word($dni)
{
return substr("TRWAGMYFPDXBNJZSQVHLCKE",strtr($dni,"XYZ","012")%23,1);
}
private function validateDNI($value)
{
$return="El campo $this->field ";
if(strlen($value)!=9)
return $this->showValidation($return."debe contener 9 caracteres");
$last=strtoupper(substr($value,-1));
$first=strtoupper(substr($value,0,1));
$numbers=str_replace("X","0",strtoupper(substr($value,0,-1)));
if(!ctype_digit($first) && $first!="X")
return $this->showValidation($return."es erróneo (revisar el primer caracter)");
if(!ctype_digit($numbers))
return $this->showValidation($return."es incorrecto");
if($this->DNI_Word($numbers)!=$last)
return $this->showValidation("La letra del campo $this->field no es correcta");
return $this->showValidation();
}
private function validateEMAIL($value)
{
$return="El campo $this->field ";
if(!filter_var($value, FILTER_VALIDATE_EMAIL))
return $this->showValidation($return."no tiene el formato correcto");
return $this->showValidation();
}
private function validateURL($value)
{
$return="El campo $this->field ";
if(!filter_var($value, FILTER_VALIDATE_URL))
return $this->showValidation($return."no tiene el formato correcto");
return $this->showValidation();
}
private function validateIP($value)
{
$return="El campo $this->field no tiene el formato correcto ";
$option="";
if(isset($this->only))
$option="FILTER_FLAG_".strtoupper($this->only);
if(!filter_var($value, FILTER_VALIDATE_IP,$option))
if(isset($this->only))
return $this->showValidation($return."(debe respetar el protocolo ".strtoupper($this->only).")");
else
return $this->showValidation($return);
return $this->showValidation();
}
private function validatePASS($value)
{
$return="El campo $this->field ";
if(isset($this->minchr) && strlen($value)<$this->minchr)
return $this->showValidation($return."ha de tener como mínimo $this->minchr caracteres");
if(isset($this->maxchr) && strlen($value)>$this->maxchr)
return $this->showValidation($return."ha de tener como máximo $this->maxchr caracteres");
if (!preg_match('`[a-z]`',$value))
return $this->showValidation($return."ha de tener como mínimo 1 letra minúscula");
if (!preg_match('`[A-Z]`',$value))
return $this->showValidation($return."ha de tener como mínimo 1 letra mayúscula");
if (!preg_match('`[0-9]`',$value))
return $this->showValidation($return."ha de tener como mínimo 1 número");
return $this->showValidation();
}
private function validateHOUR($value)
{
$return="El campo $this->field ";
$hour=explode($this->sep,$value);
if(strlen($value)!=5)
return $this->showValidation($return."no es correcto");
if(count($hour)!=2) {
$format="HH".$this->sep."MM";
return $this->showValidation($return."no tiene el formato correcto ($format)");
}
$minute=$hour[1];
$hour=$hour[0];
if(isset($this->tfh) && $this->tfh=="1") {
if($hour<0 || $hour>=24 || $minute<0 || $minute>59)
return $this->showValidation($return." no es una hora válida (ha de ser 24h)");
}
else {
if($hour<0 || $hour>=12 || $minute<0 || $minute>59)
return $this->showValidation($return." no es una hora válida (ha de ser 12h)");
}
return $this->showValidation();
}