Error in the if cycle

6
  

The dreamweaver gives me an error on line 6 and I can not find it, I'm   new in programming I appreciate your time and your help a lot.

<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>
    
asked by Fabricio 13.09.2016 в 04:51
source

5 answers

5

There are at least these failures that will make it fail, to correct them:

  • Remove the semicolon ; after if and before else if (and the same between else if and else ).
  • Instead of assigning ( = ) in the condition, you must compare ( == )
  • You must declare the variables outside the conditional structure

and that should solve the problem

// 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" 
} 
    
answered by 13.09.2016 / 05:07
source
9

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

First, the condition is poorly written. When you put the reserved word var you are declaring a variable, so in your condition you are reclassifying the two variables and equaling them to a% 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 way 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" 
    }
  } 
}
    
answered by 13.09.2016 в 08:23
4

I think the error is 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>

Remove the VAR from if, I do not think it is necessary to declare it there again, and for the conditions I think that one way to serve it better is ((mail == "") || (code == "")) separating them into another parenthesis

    
answered by 13.09.2016 в 05:11
2

A simpler way to do it 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 does value put it in the conditional instead of declaring it above?

Because when using css properties it does not create conflicts, if I want it to show a red border when there is an error it would be assigned in the conditional, not up.

I hope I have served you.

Greetings.

    
answered by 13.09.2016 в 14:06
1

People like that are the final version 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>
    
answered by 05.03.2017 в 00:51