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;
}
}