* @copyright 2010 Bartolomé Sintes Marco * @license http://www.gnu.org/licenses/agpl.txt AGPL 3 or later * @version 2010-03-25 * @link https://www.mclibre.org * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ // Función de recogida de datos function recoge($key, $type = "") { if (!is_string($key) && !is_int($key) || $key == "") { trigger_error("Function recoge(): Argument #1 (\$key) must be a non-empty string or an integer", E_USER_ERROR); } elseif ($type !== "" && $type !== []) { trigger_error("Function recoge(): Argument #2 (\$type) is optional, but if provided, it must be an empty array or an empty string", E_USER_ERROR); } $tmp = $type; if (isset($_REQUEST[$key])) { if (!is_array($_REQUEST[$key]) && !is_array($type)) { $tmp = trim(htmlspecialchars($_REQUEST[$key])); } elseif (is_array($_REQUEST[$key]) && is_array($type)) { $tmp = $_REQUEST[$key]; array_walk_recursive($tmp, function (&$value) { $value = trim(htmlspecialchars($value)); }); } } return $tmp; } function cabecera($texto) { print "\n"; print "\n"; print "\n"; print " \n"; print " \n"; print " $texto. Validación.\n"; print " Ejercicios. PHP. Bartolomé Sintes Marco. www.mclibre.org\n"; print " \n"; print " \n"; print " \n"; print "\n"; print "\n"; print "\n"; print "

$texto

\n"; print "\n"; } define("FORM_METHOD", "get"); define("TAM_NOMBRE", 40); define("TAM_TELEFONO", 9); define("TAM_CORREO", 40); $nombre = recoge("nombre"); $telefono = recoge("telefono"); $correo = recoge("correo"); $nombrePatron = "/^[[:alpha:]]+$/"; $telefonoPatron = "/^[0-9]{9}$/"; $correoPatron = "/^[0-9abcdefghijklmnopqrstuvwxyz\._-]+@" . "[0-9abcdefghijklmnopqrstuvwxyz]+(\.[abcdefghijklmnopqrstuvwxyz]+){1,2}$/"; $nombreOk = preg_match($nombrePatron, $nombre); $telefonoOk = preg_match($telefonoPatron, $telefono); $correoOk = preg_match($correoPatron, $correo); if (isset($_REQUEST["enviar"]) && $nombreOk && $telefonoOk && $correoOk) { cabecera("Validación de formulario (Resultado válido)"); print "

Los datos introducidos son correctos.

\n"; print "\n"; print "

Nombre:$nombre

\n"; print "\n"; print "

Teléfono:$telefono

\n"; print "\n"; print "

Correo:$correo

\n"; print "\n"; print "

Volver al formulario

\n"; } else { if (isset($_REQUEST["enviar"])) { cabecera("Validación de formulario (Resultado inválido)"); print"

Por favor, corrija los datos incorrectos:

\n"; print "\n"; } else { cabecera("Validación de formulario (Formulario)"); print"

Escriba los datos siguientes:

\n"; print "\n"; } print "
\n"; print " \n"; print " \n"; print " \n"; print " \n"; print " \n"; print " \n"; print " \n"; print " \n"; print " \n"; print " \n"; print " \n"; print " \n"; print " \n"; print "
Nombre:"; if (isset($_REQUEST["nombre"]) && !$nombreOk) { print " El nombre no es correcto"; } print "
Teléfono:"; if (isset($_REQUEST["telefono"]) && !$telefonoOk) { print " El teléfono no es correcto"; } print "
Correo:"; if (isset($_REQUEST["correo"]) && !$correoOk) { print " El correo no es correcto"; } print "
\n"; print "\n"; if (isset($_REQUEST["enviar"])) { print "

Volver al principio

\n"; print "\n"; } print "

\n"; print "
\n"; } ?>