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

Asked

Viewed 27 times

1

Good night. I need to create a form that the email can be filled with a valid email and password (leads to a page) or with a name without @ and no password (leads to another page). Is it possible to do this? 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-->

1 answer

0


Yes, you can call the action in the script according to the condition. I created a Event Handler to catch the submit form:

I recommend using type="email" in the e-mail field as required will require there to be at least one @.

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, erase the attribute action of <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 would look 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"; 
   }
});
  • Thank you very much, it worked perfectly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.