$(document).ready(function(){
// CLick do botao
$('.botaoEnviar').click(function(){
// no click executa a funçao que valida e verifica o estado
if(validaFormulario())
{
// se a funcção for true quer dizer que não há erros de preenchimento e executa o que está neste if
// neste caso vamos enviar os dados para um php através de jquery e receber a resposta
$.ajax({
type: "POST",
url: "form.php",
data: { primeiroNome: $('input[name=primeiroNome]').val(), apelido:$('input[name=apelido]').val(),email:$('input[name=email]').val()},
dataType: "html"
}).done(function( msg ) {
// msg é o resultado que vem do php que podemos escrever no div de status
$('.estadoDoEnvio').html(msg);
});
}
})
})
function validaFormulario()
{
var estado = true;
// variável que vai armazenar os erros
var erros = "";
$('input').each(function(){
// verifica se o data-obrigatorio é igual a sim
if($(this).data('obrigatorio') == "sim")
{
// verifica se o comprimento da string é maior que 2
if($(this).val().length < 2)
{
estado = false;
erros += $(this).data('mensagemerro')+"<br/>";
}
}
})
$('.estadoDoEnvio').html(erros);
return estado;
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.