0
Galley made a function that when the user clicks on a button, it calls the action document.form.submit()
, till then all right, it sends the form normally. But on <form>
put the onsubmit="return validarFormulario();"
but it is not calling this function and does not generate error in the console, 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>
functions js.
function validarFormulario(){
if(document.getElementById("titulo").value==""){
alert("Preencha o campo de titulo");
return false;
}
}
You have to put in more code, otherwise you can’t tell what the problem might be...
– Sergio
Post the function validateFormulario.
– Laerte
@Sergio added the codes.
– Samuel Carvalho
@Laerte added the codes.
– Samuel Carvalho
A question you put the script funcoes.js at the head or end of the body?
– Laerte
@Laerte tried in both cases, neither of them worked. I gave a
console.log("teste");
inside the file, and they are working normal, it seems that the functiononsubmit
ofform
not working.– Samuel Carvalho
You want: when the person clicks on the "Save" link to give Submit in the form but first validating if the function return is true?
– Laerte
@Laerte.
– Samuel Carvalho
The function is called, but the
if
always givesfalse
because the title field is mandatory and never empty. What’s wrong? How do you perform the function Submit? -> https://jsfiddle.net/9h9pxeay/– Sergio
@Sergio here I call the Submit function
<a onclick="document.formulario.submit();">
 <span class="icone-menu-pagina icone-salvar"></span>
 <span>Salvar</span>
 </a>
– Samuel Carvalho
Won’t you have any buttons to send the form? Just the link right? You can put the Submit() in the validation function?
– Laerte
@Laerte yes, only on the link. How I put Submit in validation?
– Samuel Carvalho
See if my answer answers what you expect.
– Laerte