document.form.submit () does not validate

0

I made a function that when the user clicks a button, it calls the action document.form.submit() , so alright, it sends the form normally. But in <form> I put onsubmit="return validarFormulario();" but it is not calling this function and it does not generate an error in the console, it just does not perform the function. How do I validate the form using this method?

index.html

<div class="cabecalho-pagina">
        <div class="titulo">
            <div class="icon-pagina">
                <img src="<?=_URL_;?>/admin/img/icon-48-article-add.png" alt="Artigo">
            </div>
            <span>Notícia: [Novo]</span>
        </div>

        <div class="menus-pagina">
            <a onclick="document.formulario.submit();">
                <span class="icone-menu-pagina icone-salvar"></span>
                <span>Salvar</span>
            </a>
            <a href="">
                <span class="icone-menu-pagina icone-cancelar"></span>
                <span>Cancelar</span>
            </a>
        </div>
    </div>

    <div class="conteudo-pagina">
        <form action="" method="post" id="formulario" enctype="multipart/form-data" class="formulario" name="formulario" onsubmit="return validarFormulario();">
            <input type="text" name="titulo" id="titulo" class="titulo" placeholder="Título da notícia" required/>
            <input type="text" name="data" id="data" class="data" value="<?=date("d-m-Y h:i:s");?>" required/>

            <label>
                Publicado:
                <select class="publicado" name="publicado" id="">
                    <option value="1" required>Sim</option>
                    <option value="1">Não</option>
                </select>
            </label>

            <textarea class="conteudo" id="conteudo" name="conteudo" placeholder="Conteudo da notícia" required></textarea>

            <input type="file" name="imagem" class="imagem" id="imagem" accept=".jpg,.png,.gif">

        </form>

    </div>

funcoes.js

function validarFormulario(){
    if(document.getElementById("titulo").value==""){
        alert("Preencha o campo de titulo");
        return false;
    }

}
    
asked by anonymous 15.04.2015 / 17:12

1 answer

1

I've removed the "required" attribute for demonstration purposes, I passed the pro validation call "Save" since the form will not have any submit button. If your function does not enter any of the validations and return false the default return is an alert and then it sends your form, the onsubmit attribute on your form does not make sense in this case where the call is made via Javascript

function validarFormulario() {
  // validações
  if (document.getElementById("titulo").value == "") {
    alert("Preencha o campo de titulo");
    return false;
  }
  alert("Enviou");
  document.formulario.submit();
}
<div class="menus-pagina">
  <a href="#" onclick="validarFormulario()">
    <span class="icone-menu-pagina icone-salvar"></span>
    <span>Salvar</span>
  </a>
  <a href="#">
    <span class="icone-menu-pagina icone-cancelar"></span>
    <span>Cancelar</span>
  </a>
</div>
</div>

<div class="conteudo-pagina">
  <form action="" method="post" id="formulario" enctype="multipart/form-data" class="formulario" name="formulario">
    <input type="text" name="titulo" id="titulo" class="titulo" placeholder="Título da notícia" />
    <br>
    <input type="text" name="data" id="data" class="data" placeholder="Data da Postagem" />
    <br>
    <label>
      Publicado:
      <select class="publicado" name="publicado" id="">
        <option value="1">Sim</option>
        <option value="1">Não</option>
      </select>
    </label>
    <br>
    <textarea class="conteudo" id="conteudo" name="conteudo" placeholder="Conteudo da notícia"></textarea>
    <br>
    <input type="file" name="imagem" class="imagem" id="imagem" accept=".jpg,.png,.gif">
  </form>
</div>
    
15.04.2015 / 17:37