Error in Javascript form validation

Asked

Viewed 410 times

6

I have a validation error when I press the button input of submit he does not test the function Javascript and goes right to where the action is telling to go.

<HTML>
<HEAD>
<TITLE>Cadastro</title>
<script language="JavaScript">
    function validaFormCadastro() {           
        d = document.form_cadastro;                      
        if (d.NOME.value == "") {                     
            alert("O campo NOME deve ser preenchido!");                    
            d.NOME.focus();                     
            return false;           
        }
        if (d.NOMEUSUARIO.value == "") {                     
            alert("O campo NOME DE USUARIO deve ser preenchido!");                    
            d.NOMEUSUARIO.focus();                     
            return false;           
        }           
        if (d.RM.value == "") {                     
            alert("O campo RM deve ser preenchido!");                    
            d.RM.focus();                     
            return false;           
        }
        if (d.SENHA.value == "") {                     
            alert("O campo SENHA deve ser preenchido!");                    
            d.SENHA.focus();                     
            return false;           
        }
    }
</script>
</head>
<body>
<form name="form_cadastro" action="salvar_aluno.php" method="get">
    <table border='1'>
        <tr>
            <td>
                <label>Nome completo</label>
            </td>
            <td>
                <input type="text" name="NOME">
                <br>
            </td>
        </tr>
        <tr>
            <td>
                <label>Nome de Usuario</label>
            </td>
            <td>
                <input type="text" name="NOMEUSUARIO">
                <br>
            </td>
        </tr>
        <tr>
            <td>
                <label>RM</label>
            </td>
            <td>
                <input type="text" name="RM">
                <br>
            </td>
        </tr>
        <tr>
            <td>
                <label>SENHA</label>
            </td>
            <td>
                <input type="password" name="SENHA">
                <br>
            </td>
        </tr>
    </table>
    <p>
        <input type="submit" name="botao_comando" value="CADASTRAR" onclick="return validaFormCadastro()">
</form>
</body>
</html>

  • 1

    Boy, your role is being loaded, both in a file I created to test, and in this Jsfiddle: http://jsfiddle.net/SamirChaves/asd7ddt3/1/ test and see if this is really your problem

  • Try to change the d = document.form_cadastro for d = document.forms.id_form_cadastro, in case, it would be an id, not the name.

  • Your form has an input type="submit", soon will ignore the existing js, and proceed with sending the form. One of the solutions would be to put a replace the event onclick of that button, for a onblur.

  • Here worked normally, probably is some other script that is error and the rest of javascript does not process.

1 answer

0

The solution is in the call of the method validaFormCadastro() Voce add the event.preventDefault(); to return the original state. Try so:

    <!-- no seu input, mande como parametro o contexto do seu botao -->
    <input type="submit" name="botao_comando" value="CADASTRAR" onclick="validaFormCadastro(this)">

    <script>
    function validaFormCadastro(event) {   
        //metodo para voltar ao estado original (vai ignorar onsubmit da funcao)
        event.preventDefault();  

        d = document.form_cadastro;                      
        if (d.NOME.value == "") {                     
            alert("O campo NOME deve ser preenchido!");                    
            d.NOME.focus();                     
            return false;           
        }
        if (d.NOMEUSUARIO.value == "") {                     
            alert("O campo NOME DE USUARIO deve ser preenchido!");                    
            d.NOMEUSUARIO.focus();                     
            return false;           
        }           
        if (d.RM.value == "") {                     
            alert("O campo RM deve ser preenchido!");                    
            d.RM.focus();                     
            return false;           
        }
        if (d.SENHA.value == "") {                     
            alert("O campo SENHA deve ser preenchido!");                    
            d.SENHA.focus();                     
            return false;           
        }
    }
</script>

Browser other questions tagged

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