Redirect to one page if email is wrong and another one is right

1

Good evening. I need to create a form that the email can be filled with a valid e-mail and password (leads to a page) or with a name without @ and without password (leads to another page). Is it possible to do that? At the moment I have this code that directs both options to the same page.

<div id="login">
    <span>Acesso Cond&ocirc;mino</span>




<form target="_blank" name="form1" method="post" action="http://sistema.ajmcondominios.com.br">

      <input type="hidden" value="logar" name="action"/>
      <input id="cod" type="hidden" value="845" name="cod"/>


 <label>Login:
<input type="text"  name="login" id="nome" value="" placeholder="Digite seu Login..." required/></label>
<label style="margin-left:30px;">Senha:
<input type="password" name="senha" id="senha" value="" placeholder="Digite sua senha..." /></label>
<label style="margin-left:30px;margin-top:30px;">
<button name="Submit" type="submit" value=“” style=“medium”>OK</button></label>
    </form>
</div><!--fim login-->
    
asked by anonymous 18.01.2018 / 23:54

1 answer

0

Yes, you can call action in the script according to the condition. I created an event handler to get the submit of the form:

  

I recommend using type="email" in the email field because the    required will require that there is at least @ .

document.form1.addEventListener("submit", function(){
   if(this.login.value.indexOf("@") == -1 || !this.senha.value){
     // página com erro, email sem @ OU campo senha vazio
     document.form1.action = "pagina1.php"; 
   }else{
     document.form1.action = "pagina2.php"; 
   }
});

Now, delete the action attribute from <form> :

<form target="_blank" name="form1" method="post">

EDIT

If you really want to validate the email, you can use this function:

function checkMail(email){
    invalidChars = " ~\'^\'\"*+=\|][(){}$&!%/:,;ç";

    if (email == "") {
        return false;
    }

    for (i=0; i<invalidChars.length; i++) {
        badChar = invalidChars.charAt(i);
        if (email.indexOf(badChar,0) > -1) {
            return false;
        }
    }

    lengthOfEmail = email.length;
    if ((email.charAt(lengthOfEmail - 1) == ".") || (email.charAt(lengthOfEmail - 2) == ".")) {
        return false;
    }

    Pos = email.indexOf("@",1);
    if (email.charAt(Pos + 1) == ".") {
        return false;
    }

    while ((Pos < lengthOfEmail) && ( Pos != -1)) {
        Pos = email.indexOf(".",Pos);
        if (email.charAt(Pos + 1) == ".") {
            return false;
        }
        if (Pos != -1) {
            Pos++;
        }
    }

    atPos = email.indexOf("@",1);
    if (atPos == -1) {
        return false;
    }

    if (email.indexOf("@",atPos+1) != -1) {
        return false;
    }

    periodPos = email.indexOf(".",atPos);
    if (periodPos == -1) {
        return false;
    }

    if (periodPos+3 > email.length) {
        return false;
    }
    return true;
}

In this case the code looks like this:

document.form1.addEventListener("submit", function(){
   if(!checkMail(this.login.value) || !this.senha.value){
     // página com erro, email inválido OU campo senha vazio
     document.form1.action = "pagina1.php"; 
   }else{
     document.form1.action = "pagina2.php"; 
   }
});
    
19.01.2018 / 00:12