<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Validación en PHP</title>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
// Variables que contendrán un posible mensaje de error
$nameErr = $emailErr = $genderErr = $websiteErr = "";
// Variables que guardan el contenido de los campos del formulario
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Nombre necesario";
} else {
$name = test_input($_POST["name"]);
// verificar si el nombre contiene solo letras y espacios en blanco
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Solo se permiten letras y espacios en blanco";
}
}
if (empty($_POST["email"])) {
$emailErr = "Email necesario";
} else {
$email = test_input($_POST["email"]);
// verificar si la direccion de correo es valida
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) {
$emailErr = "Formato de dirección inválido";
}
}
if (empty($_POST["website"])) {
$website = "";
} else {
$website = test_input($_POST["website"]);
// verifica si la URL es válida (la expresion regular también permite guiones en la URL)
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
$websiteErr = "URL con capacidades diferentes";
}
}
if (empty($_POST["comment"])) {
$comment = "";
} else {
$comment = test_input($_POST["comment"]);
}
if (empty($_POST["gender"])) {
$genderErr = "Genero necesario";
} else {
$gender = test_input($_POST["gender"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<h2>Ejemplo de Validación de Formularios con PHP</h2>
<p><span class="error">* campo requerido.</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Nombre: <input type="text" name="name" value="<?php echo $name;?>">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
E-mail: <input type="text" name="email" value="<?php echo $email;?>">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
Website: <input type="text" name="website" value="<?php echo $website;?>">
<span class="error"><?php echo $websiteErr;?></span>
<br><br>
Comentario: <textarea name="comment" rows="5" cols="40"></textarea>
<br><br>
Genero:
<input type="radio" name="gender"
<?php if (isset($gender) && $gender=="femenino") echo "checked";?>
value="femenino">Femenino
<input type="radio" name="gender"
<?php if (isset($gender) && $gender=="masculino") echo "checked";?>
value="masculino">Masculino
<span class="error">* <?php echo $genderErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
echo "<h2>Usted Ingresó:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;
?>
</body>
</html>
- Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.7
- <!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Validación en PHP</title>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
Warning: Undefined array key "REQUEST_METHOD" in /in/VsFH1 on line 31
<h2>Ejemplo de Validación de Formularios con PHP</h2>
<p><span class="error">* campo requerido.</span></p>
<form method="post" action="/in/VsFH1">
Nombre: <input type="text" name="name" value="">
<span class="error">* </span>
<br><br>
E-mail: <input type="text" name="email" value="">
<span class="error">* </span>
<br><br>
Website: <input type="text" name="website" value="">
<span class="error"></span>
<br><br>
Comentario: <textarea name="comment" rows="5" cols="40"></textarea>
<br><br>
Genero:
<input type="radio" name="gender"
value="femenino">Femenino
<input type="radio" name="gender"
value="masculino">Masculino
<span class="error">* </span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<h2>Usted Ingresó:</h2><br><br><br><br>
</body>
</html>
- Output for 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.24 - 5.5.35, 5.6.7 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.12 - 7.3.31, 7.4.0 - 7.4.33
- <!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Validación en PHP</title>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
Notice: Undefined index: REQUEST_METHOD in /in/VsFH1 on line 31
<h2>Ejemplo de Validación de Formularios con PHP</h2>
<p><span class="error">* campo requerido.</span></p>
<form method="post" action="/in/VsFH1">
Nombre: <input type="text" name="name" value="">
<span class="error">* </span>
<br><br>
E-mail: <input type="text" name="email" value="">
<span class="error">* </span>
<br><br>
Website: <input type="text" name="website" value="">
<span class="error"></span>
<br><br>
Comentario: <textarea name="comment" rows="5" cols="40"></textarea>
<br><br>
Genero:
<input type="radio" name="gender"
value="femenino">Femenino
<input type="radio" name="gender"
value="masculino">Masculino
<span class="error">* </span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<h2>Usted Ingresó:</h2><br><br><br><br>
</body>
</html>
- Output for 7.3.32 - 7.3.33
- <!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Validación en PHP</title>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<h2>Ejemplo de Validación de Formularios con PHP</h2>
<p><span class="error">* campo requerido.</span></p>
<form method="post" action="/in/VsFH1">
Nombre: <input type="text" name="name" value="">
<span class="error">* </span>
<br><br>
E-mail: <input type="text" name="email" value="">
<span class="error">* </span>
<br><br>
Website: <input type="text" name="website" value="">
<span class="error"></span>
<br><br>
Comentario: <textarea name="comment" rows="5" cols="40"></textarea>
<br><br>
Genero:
<input type="radio" name="gender"
value="femenino">Femenino
<input type="radio" name="gender"
value="masculino">Masculino
<span class="error">* </span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<h2>Usted Ingresó:</h2><br><br><br><br>
</body>
</html>
- Output for 5.2.3 - 5.2.17
- <!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Validación en PHP</title>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
Notice: Undefined index: REQUEST_METHOD in /in/VsFH1 on line 31
<h2>Ejemplo de Validación de Formularios con PHP</h2>
<p><span class="error">* campo requerido.</span></p>
<form method="post" action="/in/VsFH1">
Nombre: <input type="text" name="name" value="">
<span class="error">* </span>
<br><br>
E-mail: <input type="text" name="email" value="">
<span class="error">* </span>
<br><br>
Website: <input type="text" name="website" value="">
<span class="error"></span>
<br><br>
Comentario: <textarea name="comment" rows="5" cols="40"></textarea>
<br><br>
Genero:
<input type="radio" name="gender"
value="femenino">Femenino
<input type="radio" name="gender"
value="masculino">Masculino
<span class="error">* </span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<h2>Usted Ingresó:</h2><br><br><br><br>
</body>
</html>
- Output for 4.3.0 - 4.3.11, 4.4.0 - 4.4.9, 5.0.0 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.2
- <!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Validación en PHP</title>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
Notice: Undefined index: REQUEST_METHOD in /in/VsFH1 on line 31
<h2>Ejemplo de Validación de Formularios con PHP</h2>
<p><span class="error">* campo requerido.</span></p>
<form method="post" action="">
Nombre: <input type="text" name="name" value="">
<span class="error">* </span>
<br><br>
E-mail: <input type="text" name="email" value="">
<span class="error">* </span>
<br><br>
Website: <input type="text" name="website" value="">
<span class="error"></span>
<br><br>
Comentario: <textarea name="comment" rows="5" cols="40"></textarea>
<br><br>
Genero:
<input type="radio" name="gender"
value="femenino">Femenino
<input type="radio" name="gender"
value="masculino">Masculino
<span class="error">* </span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<h2>Usted Ingresó:</h2><br><br><br><br>
</body>
</html>
preferences:
213.75 ms | 405 KiB | 316 Q