Interesante este enlace que ha pasado en su twitter @AlexPuig, donde explican 25 formas de validación con PHP en códigos realmente simples y cortos.
Entre las validaciones estan las siguientes:
Tabla de contenidos
Validar e-mail en PHP
function isValidEmail($email){
return eregi('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$', $email);
}
PHP 5.2 o posterior
function fnValidateEmail($email)
{
return filter_var($email, FILTER_VALIDATE_EMAIL);
}
Validar que el e-mail exista
function check_email($email)
{
$email_error = false;
$Email = htmlspecialchars(stripslashes(strip_tags(trim($email)))); //parse unnecessary characters to prevent exploits
if ($Email == '') { email_error = true; }
elseif (!eregi('^([a-zA-Z0-9._-])+@([a-zA-Z0-9._-])+\.([a-zA-Z0-9._-])([a-zA-Z0-9._-])+', $Email)) { email_error = true; }
else {
list($Email, $domain) = split('@', $Email, 2);
if (! checkdnsrr($domain, 'MX')) { email_error = true; }
else {
$array = array($Email, $domain);
$Email = implode('@', $array);
}
}
if (email_error) { return false; } else{return true;}
}
Validar números
function fnValidateNumber($value)
{
#is_ double($value);
#is_ float($value);
#is_ int($value);
#is_ integer($value);
return is_numeric($value);
}
PHP 5.2 o posterior
function fnValidateNumber($value)
{
#return filter_var($value, FILTER_VALIDATE_FLOAT); // float
return filter_var($value, FILTER_VALIDATE_INT); # int
}
Validar un String
function fnSanitizeStringr($str)
{
#letters and space only
return preg_match('/[^A-Za-z\s ]/', '', $str);
}
Validar carácteres alfanuméricos
function fnValidateAlphanumeric($string)
{
return preg_match('/[^a-zA-Z0-9\s]/', '', $string);
}
Validar que la URL exista
function url_exist($url)
{
$url = @parse_url($url);
if (!$url)
{
return false;
}
$url = array_map('trim', $url);
$url['port'] = (!isset($url['port'])) ? 80 : (int)$url['port'];
$path = (isset($url['path'])) ? $url['path'] : '';
if ($path == '')
{
$path = '/';
}
$path .= (isset($url['query'])) ? '?$url[query]' : '';
if (isset($url['host']) AND $url['host'] != @gethostbyname($url['host']))
{
if (PHP_VERSION >= 5)
{
$headers = @get_headers('$url[scheme]://$url[host]:$url[port]$path');
}
else
{
$fp = fsockopen($url['host'], $url['port'], $errno, $errstr, 30);
if (!$fp)
{
return false;
}
fputs($fp, 'HEAD $path HTTP/1.1\r\nHost: $url[host]\r\n\r\n');
$headers = fread($fp, 4096);
fclose($fp);
}
$headers = (is_array($headers)) ? implode('\n', $headers) : $headers;
return (bool)preg_match('#^HTTP/.*\s+[(200|301|302)]+\s#i', $headers);
}
return false;
}
Validar formato de URL
function fnValidateUrl($url){
return preg_match('/^(http(s?):\/\/|ftp:\/\/{1})((\w+\.){1,})\w{2,}$/i', $url);
}
Validar que el password sea fuerte
function fnValidatePassword($password){
#must contain 8 characters, 1 uppercase, 1 lowercase and 1 number
return preg_match('/^(?=^.{8,}$)((?=.*[A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z]))^.*$/', $password);
}
Validar tarjeta de crédito
function fnValidateCreditCard($cc){
#eg. 718486746312031
return preg_match('/^(?: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})$/', $cc);
}
Validar fechas en PHP
Valida si la fecha es MM-DD-YYYY o MM-DD-YY
function fnValidateDate($date){
#05/12/2109
#05-12-0009
#05.12.9909
#05.12.99
return preg_match('/^((0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[- /.][0-9]?[0-9]?[0-9]{2})*$/', $date);
}
Para ver todos las 25 validaciones en PHP teneis que acceder a este enlace
Un comentario
Gracias por el aporte!!
Me gustaría que explicases como conectar la base de datos con un formulario…
Saludos!