Skip to content

Instantly share code, notes, and snippets.

@JaviPedrera
Last active November 19, 2015 14:23
Show Gist options
  • Save JaviPedrera/b6798d8b4a7d1478cb54 to your computer and use it in GitHub Desktop.
Save JaviPedrera/b6798d8b4a7d1478cb54 to your computer and use it in GitHub Desktop.
<?php
if (isset($_POST['submit'])) {
// Empty array for errors
$errors = array();
// Name validation
if (empty($_POST['name'])) {
$errors['name'] = "Debe completar";
} else {
if (strlen($_POST['name']) < 7 || is_numeric($_POST['name']) ) {
$errors['name'] = "El nombre debe tener una longitud mínima de 7 letras.";
}
}
// Mail validation
if (empty($_POST['mail'])) {
$errors['mail'] = "Debe completar";
} else {
if ( ! filter_var($_POST['mail'], FILTER_VALIDATE_EMAIL) ) {
$errors['mail'] = "El correo debe ser válido.";
}
}
// Phone validation
if (empty($_POST['phone'])) {
$errors['phone'] = "Debe completar";
} else {
if ( ! preg_match("/^[0-9]{9}$/", $_POST['phone'])) {
$errors['phone'] = "El teléfono ha de ser válido";
}
}
// Check validation
if ( ! isset($_POST['check'])) {
$errors['check'] = "Has de aceptar los términos y condiciones";
}
// If validation passes
if (empty($errors)) {
// Database query
// Redirect
$success = "Se ha registrado correctamente!";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Coloors Validation Form</title>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<!-- COL-LG-6 OFFSET-3 -->
<div class="col-lg-6 col-lg-offset-3" style="margin-top: 100px;">
<?php if (isset($success)) { ?>
<div class="alert alert-success">
<h3><?php echo $success ?></h3>
<ul>
<li><b>Name:</b> <?php echo $_POST['name']?></li>
<li><b>Mail:</b> <?php echo $_POST['mail']?></li>
<li><b>Phone:</b> <?php echo $_POST['phone']?></li>
</ul>
</div><!-- end alert alert-success -->
<?php } ?>
<h1 class="jumbotron text-center" style="background-color: #3E3E8E; color: white;">Formulario</h1>
<!-- FORM -->
<form action="index.php" method="POST">
<div class="form-group <?php if ($errors['name']) echo 'has-error' ?>">
<label for="name">Name</label>
<input class="form-control" type="text" name="name" />
<?php if (isset($errors['name'])) { ?>
<span class="help-block"><?php echo $errors['name'] ?></span>
<?php } ?>
</div><!-- end form-group -->
<div class="form-group <?php if ($errors['mail']) echo 'has-error' ?>">
<label for="mail">Mail</label>
<input class="form-control" type="text" name="mail" />
<?php if (isset($errors['mail'])) { ?>
<span class="help-block"><?php echo $errors['mail'] ?></span>
<?php } ?>
</div><!-- end form-group -->
<div class="form-group <?php if ($errors['phone']) echo 'has-error' ?>">
<label for="phone">Phone</label>
<input class="form-control" type="text" name="phone" />
<?php if (isset($errors['phone'])) { ?>
<span class="help-block"><?php echo $errors['phone'] ?></span>
<?php } ?>
</div><!-- end form-group -->
<div class="form-group <?php if ($errors['check']) echo 'has-error' ?>">
<p><input type="checkbox" name="check" value="1" /> Acepto términos y condiciones</p>
<?php if (isset($errors['check'])) { ?>
<span class="help-block"><?php echo $errors['check'] ?></span>
<?php } ?>
</div><!-- end form-group -->
<div class="form-group">
<input type="submit" name="submit" class="btn btn-primary" />
</div><!-- end form-group -->
</form>
</div><!-- end col-lg-6 -->
</div><!-- end row -->
</div><!-- end container -->
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment