If loop Error

The dreamweaver gives me an error on Line 6 and I can't find it, I'm new to programming I really appreciate your time and your help.

<script>
window.onload = function(){
    var envia = document.getElementById(continuar);
    envia.onclick = function(){
        var mail = mail.value;
        var codigo = codigoPostal.value;
        if(var mail = "" || var codigo = ""){
        alert("Mail y código postal vacíos")
        };
        else if (mail.length<=5){
            alert ("Tu mail es muy corto")};
            else {alert("El formulario se ha llenado de forma correcta")
            window.location.href = "pago.html" }

        } 
}

</script>
 6
Author: Fabricio, 2016-09-13

5 answers

There are at least these bugs that will make it fail, to fix them:

  • removes the semicolon ; that exists after if and before else if (and the same between else if and else).
  • instead of assigning (=) in the condition, you should compare (==)
  • you must declare variables outside the conditional structure

And that should fix the

// por aquí declararías mail y codigo

if(mail == "" || codigo == ""){
    alert("Mail y código postal vacíos")
} else if (mail.length<=5){
    alert ("Tu mail es muy corto")
} else {
    alert("El formulario se ha llenado de forma correcta")
    window.location.href = "pago.html" 
} 
 5
Author: Alvaro Montoro, 2016-09-13 10:43:25

From what I see in your code several things are happening:

First, the condition is misspelled. When you put the reserved word var you are declaring a variable, so in your condition you are re-declaring the two variables and equating them to an empty string.

var valor = 'mi valor'    // declaras variable
valor = 'mi nuevo valor'  // sobrescritas variable

Another point is that the equality operator is expressed with == or with === in strict mode, so if you wanted to compare two values the correct mode would be like this:

if (valor1 === valor2) { ... } // Son iguales

Finally, you have a semicolon ; after the closing brackets of if and before else if, this will cause an error.

Your code would look like this:

window.onload = function () {
  var envia = document.getElementById(continu,ar);
  envia.onclick = function(){
    // Declaras las variables
    var mail = mail.value;
    var codigo = codigoPostal.value;

    if(mail == "" || coding == ""){
      alert("Mail y código postal vacíos")
    } else if (mail.length <= 5){
      alert ("Tu mail es muy corto")
    } else {
      alert("El formulario se ha llenado de forma correcta")
      window.location.href = "pago.html" 
    }
  } 
}
 9
Author: eledgaar, 2016-09-13 13:02:18

I think the error is to redeclare the variables in the if

<script>
 window.onload = function(){
var envia = document.getElementById(continuar);
envia.onclick = function(){
    var mail = mail.value;
    var codigo = codigoPostal.value;
   if((mail == "") || (codigo == "")){
    alert("Mail y código postal vacíos")
    }
    else if (mail.length<=5){
        alert ("Tu mail es muy corto")}
        else {alert("El formulario se ha llenado de forma correcta")
        window.location.href = "pago.html" }

    } 
}

</script>

Removes the VAR from the if, I think it doesn't need to be declared there again, and for conditions I think one way it serves better is ((mail =="") / / (code =="")) separating them within another parenthesis

 4
Author: Victor Alejandro Alvarado Vilo, 2016-09-13 03:11:19

A simpler way to do this would be as follows:

   
    // Boton de envío.
    var form = document.getElementById('validate');
    // Le asignamos un Listener, donde click hace referencia al onclick que  activará la función validar.
    form.addEventListener('click', validar, false);
    // Verifica que todo este correcto.
    function validar(mail, cp) {
        mail = document.getElementById('mail');
        cp = document.getElementById('cp');
        // Verificamos los campos.
        if (mail.value === '' || cp.value === '') {
            window.alert('Campo vacio');
            return false;
        } else if (mail.length <= 5) {
            window.alert('Email muy corto');
        } else {
            window.alert('Enviado correctamente');
            // Aquí dara un error por que obviamente no existe la ruta ni el 
            // Archivo form.html, debes de introducir tu ruta.
            window.location.href = 'form.html';
        }
    }
<input type="text" id="mail" name="mail" placeholder="Dirección de email">
<input type="text" id="cp" name="cp" placeholder="Código postal">
<input type="button" id="validate" name="validate" value="Enviar">

This way you avoid putting window.onload = function () {Code}.

Why do I put the value in the conditional instead of declaring it above?

Because when using css properties it does not create conflicts, if I want that when there is an error it shows a red border I would assign it in the conditional, not above.

I hope served you.

Greetings.

 2
Author: UnderZero_-, 2016-09-13 16:12:30

People like this the final version is already serving, greetings:

<script>
window.onload = function (){
//Instancias
var continuar = document.getElementById('continuar');
var mail = document.getElementById('mail');
var codigoPostal = document.getElementById('codigoPostal');
//Listeners para detectar el envio de la forma
continuar.onclick = function(){

//Recuperamos los dos valores de cada caja
var valor1 = mail.value;
var valor2 = codigoPostal.value;

//Verificamos que no esten vacios los campos
if (mail.value === '' || codigoPostal.value === '') {
            window.alert('Mail y código postal vacíos');
            return false;
        } else if (mail.length <= 5) {
            window.alert('Tu mail es muy corto');
        } else {
            alert("El formulario se ha llenado de forma correcta");
            window.location.href = "pago.html" ;
            return false;
            }
        } 
}
</script>
 1
Author: Cesar Alonso Morera Alpizar, 2017-03-04 23:51:32