Hello, I have a contact form and I have managed to validate that the fields were filled in, but I don't know how to make the email have the correct format and if not throw an error, how can I verify that the content in that field contains @, or how to do that validation? I pass what I have:
HTML
<div id="contact_form">
<form action="confFormulario3.php" id="form1" name="form1" method="post">
<input type="text" id="email" class="boton" name="email" placeholder="Tu email">
<input type="text" id="nombre" class="boton" name="nombre" placeholder="Cómo te llamas">
<textarea name="mensaje" id="mensaje" class="boton" placeholder="En qué podemos ayudarte?"></textarea>
<input type="submit" name="Submit" id="buttonEnviar" class="boton" value="ENVIAR">
</form>
</div>
PHP
<?php
/*Capturamos las variables del POST con operadores ternarios*/
$email= ( empty($_POST["email"]) ) ? NULL : $_POST["email"];
$name= ( empty($_POST["nombre"]) ) ? NULL : $_POST["nombre"];
$message= ( empty($_POST["mensaje"]) ) ? NULL : $_POST["mensaje"];
/*Verificamos que los tres datos fueron posteados*/
if ($email && $name && $message){
$para = '[email protected]';
$asunto = "mensaje de la web de el punto!!!!!!!!!!!!";
$mailheader = "From: ".$email."\r\n";
$mailheader .= "Reaply-To:".$email."\r\n";
$mailheader .= "Content-type: text/html; charset=utf-8\r\n";
$MESSAGE_BODY = "Nombre: ".$name."\n";
$MESSAGE_BODY .= "\n<br>Email: ".$email."\n";
$MESSAGE_BODY .= "\n<br>Mensaje: ".nl2br($message)."\n";
mail($para, $asunto, $MESSAGE_BODY, $mailheader) or die("error al enviar mensaje, intente nuevamente");
echo "<script>
alert('Gracias por tu contacto! en breves nos estaremos comunicando 1');
</script>";
}else{
//Aquí puedes también redirigir con un mensaje de error
echo "<script>alert('Controla la informacion ingresada, el mensaje NO se ha enviado');</script>";
}
?>
In this type of case, it is convenient to do a double validation: on the client side and on the server side.
On the client side
Since HTML5 there is the type
email
for the elementsinput
, so you put this:It will validate the email on the client side.
server side
You can use
filter_var
to validate the email.Since it
filter_var
returns the validated data orFALSE
, you can therefore capture the email directly like this:And then the
if
would be as you have it originally in the code:To solve the problem you describe there are two ways to do it.
First option:
In this case, the simplest thing you can do is change the attribute of the input where the email is typed in this way
type="text"
bytype="email"
.Second option
Use the Jquery Validation plugin with which you validate the form and in addition to that you can show your own error messages.
Here I leave you the code of how your form would look applying the Jquery Validation plugin
1° Have the HTML form
2° Create a Javascript file where the corresponding validations are performed, which I will call
validacion.js
NOTE: For this to work, don't forget to include the plugin library and make the call
validacion.js
inside the file where your HTML form is located, otherwise it won't work. I recommend you read the documentation on its official page since the example that I show you is a basic one of the many things you can do.https://jqueryvalidation.org/documentation/
Perhaps this can help you to do the validations you need and also to insert into your database:
HTML (contact.html)
JS (validations.js)
In your php file, all you have to do is take the data and insert it into your database, the rest (validations) leave it to the JS file. I hope it helps you !