Problems validating contact form fields

1

I have a page to contact me and I need all fields filled out. I have to use alert to warn that you have not entered any field and what is that field. I tried to use isNaN but it did not work for me.

Also try this but it did not work quite well:

function validar(name, email, msg)
{
    if (name="")
    {
        alert(No has introducido tu nombre)
    }
    else if (email="")
    {
        alert("No has introducido tu email")
    }
    else if (msg="")
    {
        alert("no has introducido ningún mensaje")
    }
    else
    {
        alert("El mensaje ha sido enviado correctamente")
    }
} 
    
asked by Luca 29.06.2017 в 02:38
source

3 answers

1

You are starting with JavaScript, check this link: link

The comparisons are made using double sign and triple sign equal as you can see in the link

 function validar(name, email, msg)
      {
        if (name==="")
        {
          alert(No has introducido tu nombre)
        }
        else if (email==="")
        {
          alert("No has introducido tu email")
        }
        else if (msg==="")
        {
          alert("no has introducido ningún mensaje")
        }
        else
        {
          alert("El mensaje ha sido enviado correctamente")
        }
      } 
    
answered by 29.06.2017 в 02:51
1

In JavaScript, an equal sign is used to assign a value to a variable. That is why the code you put in the question does not work, that is, you are assigning values to a variable instead of making comparisons.

As for the way to make comparisons in JavaScript, there are two types, the abstract comparison and the strict comparison.

In an abstract comparison, the operands on the left and on the right could be converted following a JavaScript algorithm. Some recommend avoiding the use of this comparison because it is confusing.

In a strict comparison, the operands on the left and on the right do not are converted.

  • For the abstract comparison == is used.
  • Strict comparison uses === .
answered by 29.06.2017 в 06:05
0

For each field to be required you just have to add the required attribute in the field you want. Example:

<input type="text" id="nombre" required>

But if what you want is to validate the fields yourself, then the code would be more or less like this:

$(document).ready(function () {
var nombre = $('#nombre');
var correo = $('#correo');
var msg = $('#msg');

$('#aceptar').click(function (event) {
    if(nombre.val() === ""){
        event.preventDefault();
        alert('el nombre es requerido');
        console.log(nombre);
    }else if(correo.val() === ""){
        alert('el correo es requerido');
        event.preventDefault();
    }else if(msg.val() === ""){
        alert('el mensaje es requerido');
        event.preventDefault();
    }
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="post">
    <label>
        Nombre:
        <input type="text" id="nombre" required>
    </label>
    <label>
        Correo:
        <input type="email" id="correo">
    </label>
    <label>
        Mensaje:
        <input type="text" id="msg">
    </label>
    <input type="submit" value="Aceptar" id="aceptar">
</form>
Note: here I use the library of JQuery . For more information about this library go to the official page     
answered by 29.06.2017 в 04:02