Informações pessoais









Informações adicionais Interesses:







", "answerCount": 6, "upvoteCount": 2, "dateCreated": "2014-09-04T14:24:01.307Z", "author": { "@type": "Person", "name": "Lucas Carvalho", "url": "https://www.passeidireto.com/perfil/4030-lucas-carvalho" }, "suggestedAnswer": [ { "@type": "Answer", "text": "O seu proble está nos atributos NAME dos imputs o javascript não interpreta os espaços, tente alterar name=\"Estado civil\" por name=\"Estado_civil\" e name=\"Data de nascimento\" por name=\"Data_de_nascimento\" que funcionará. Boa sorte nos estudos.\nsegue o códico alterado e funcionando.\n \nSocial Mids\n\n\n
Informações pessoais\n
\n


\n





\n
\n
\nInformações adicionaisInteresses:






\n
\n
\n", "dateCreated": "2014-09-04T17:53:08.637Z", "upvoteCount": 2, "url": "https://www.passeidireto.com/pergunta/4308085/o-que-ta-de-errado-para-validar-com-javascript", "author": { "@type": "Person", "name": "Wesley Alvarenga", "url": "https://www.passeidireto.com/perfil/3994-wesley-alvarenga" } }, { "@type": "Answer", "text": "vlw obg!!", "dateCreated": "2014-09-04T19:16:47.860Z", "upvoteCount": 1, "url": "https://www.passeidireto.com/pergunta/4308085/o-que-ta-de-errado-para-validar-com-javascript", "author": { "@type": "Person", "name": "Lucas Carvalho", "url": "https://www.passeidireto.com/perfil/4030-lucas-carvalho" } }, { "@type": "Answer", "text": "o cód foi testado incremental, e no final parou de funcionar ?\nacho uma boa comentar o JS inteiro, e ir separando por partes, até ver onde para de funcionar =)\ntestei de leve aqui fazendo teste no cod e a parte de validar o form funciona até antes do var marcado = false, \ndescomentando o cod do marcado o seu js para =)\nfuncionou bem", "dateCreated": "2014-09-04T14:47:54.610Z", "upvoteCount": 0, "url": "https://www.passeidireto.com/pergunta/4308085/o-que-ta-de-errado-para-validar-com-javascript", "author": { "@type": "Person", "name": "Alberto Yamamoto", "url": "https://www.passeidireto.com/perfil/2989-alberto-yamamoto" } } ] }, "isAccessibleForFree": "True", "hasPart": { "@type": "WebPageElement", "isAccessibleForFree": "True", "cssSelector": ".answer-paywall-container" } }
Buscar

o que ta de errado para validar com javascript??

<html>
<head>
<title>Social Mids</title>

<script type="text/javascript">
function validaForm(){
var cad = document.getElementById("cadastro");


for(i = 0; i < cad.length; i++){
if(cad.elements[i].value == ''){
alert("O campo " + cad.elements[i].name + " é obrigatório!");
cad.elements[i].focus();
return false;
}
}


var marcado = false;
for(i = 0; i < cad.sexo.length; i++){
if(cad.sexo[i].checked){
marcado = true;
}
}
if(!marcado){
alert("Selecione o sexo");
return false;
}

var marcado = false;
for(i = 0; i < cad.Estado civil.length; i++){
if(cad.Estado civil[i].checked){
marcado = true;
}
}
if(!marcado){
alert("Selecione o Estado civil");
return false;
}

}

function validaNumeros() {
tecla = event.keyCode;
if (tecla >= 48 && tecla <= 57){
return true;
} else {
return false;
}
}

function validaEmail(){
var cad = document.getElementById("cadastro");

if (cad.Data de nascimento.value.indexOf("@") == -1 || cad.email.value.indexOf(".com") == -1){
alert("E-mail inválido!");
return false;
}
}

</script>
<meta charset="utf-8">
</head>

<body>

<form id="cadastro" action="" onsubmit="return validaForm();" method="post">
<fieldset>
<legend>Informações pessoais</legend>

<label> Nome: <input type="text" name="nome"/></label>
<label> Data de Nascimento:<input type="text" name="Data de nascimento" onkeypress="return validaNumeros();"/></label>
<label>Senha: <input type="password" name="senha"/></label><br />

<label>Sexo: </label><br />
<label><input type="radio" name="sexo" value="masculino"/>Masculino</label><br/>
<label><input type="radio" name="sexo" value="feminino"/>Feminino</label><br/>

<label>Estado civil:</label><br />
<label><input type="radio" name="Estado civil" value="Solteiro"/>Solteiro</label><br/>
<label><input type="radio" name="Estado civil" value="Casado"/>Casado</label><br/>
<label><input type="radio" name="Estado civil" value="Separado"/>Separado</label><br/>
<label><input type="radio" name="Estado civil" value="Divorciado"/>Divorciado</label><br/>
<label><input type="radio" name="Estado civil" value="Viuvo"/>Viuvo</label><br/>

<label>Insira uma foto de perfil:<br />
<label><input type="file" name="Foto"/></label><br />

<label> Email: <input type="text" name="Email" onchange="return validaEmail()"/></label>

</fieldset>

<fieldset>

<legend>Informações adicionais</legend>
Interesses:<br />
<label><input type="checkbox" name="Interesses" value="Filmes" />Filmes</label><br />
<label><input type="checkbox" name="Interesses" value="Musicas" />Musicas</label><br />
<label><input type="checkbox" name="Interesses" value="Séries" />Séries</label><br />
<label><input type="checkbox" name="Interesses" value="Animes" />Animes</label><br />
<label><input type="checkbox" name="Interesses" value="Livros" />Livros</label><br />
<label><input type="checkbox" name="Interesses" value="Cultura" />Cultura</label><br />

</fieldset>

<form action="">
<input type="submit" name="botao" value="Enviar"/>
<input type="reset" name="botao" value="Limpar"/>
<input type="button" name="botao" value="OK"/><br />

</body>

💡 6 Respostas

User badge image

Wesley Alvarenga

O seu proble está nos atributos NAME dos imputs o javascript não interpreta os espaços, tente alterar name="Estado civil" por name="Estado_civil" e name="Data de nascimento" por name="Data_de_nascimento" que funcionará. Boa sorte nos estudos.

segue o códico alterado e funcionando.

 

<html>
<head>
<title>Social Mids</title>

<script type="text/javascript">
function validaForm(){
var cad = document.getElementById("cadastro");


for(i = 0; i < cad.length; i++){
if(cad.elements[i].value === ''){
alert("O campo " + cad.elements[i].name + " é obrigatório!");
cad.elements[i].focus();
return false;
}
}


var marcado = false;
for(i = 0; i < cad.sexo.length; i++){
if(cad.sexo[i].checked){
marcado = true;
}
}
if(!marcado){
alert("Selecione o sexo");
return false;
}

var marcado = false;

for(i = 0; i < cad.Estado_civil.length; i++){
if(cad.Estado_civil[i].checked){
marcado = true;
}
}
if(!marcado){
alert("Selecione o Estado civil");
return false;
}

}

function validaNumeros() {
tecla = event.keyCode;
if (tecla >= 48 && tecla <= 57){
return true;
} else {
return false;
}
}

function validaEmail(){
var cad = document.getElementById("cadastro");

if (cad.Data_de_nascimento.value.indexOf("@") == -1 || cad.email.value.indexOf(".com") == -1){
alert("E-mail inválido!");
return false;
}
}

</script>
<meta charset="utf-8">
</head>

<body>

<form id="cadastro" action="" onsubmit="validaForm();" method="post">
<fieldset>
<legend>Informações pessoais</legend>

<label> Nome: <input type="text" name="nome"/></label>
<label> Data de Nascimento:<input type="text" name="Data_de_nascimento" onkeypress="return validaNumeros();"/></label>
<label>Senha: <input type="password" name="senha"/></label><br />

<label>Sexo: </label><br />
<label><input type="radio" name="sexo" value="masculino"/>Masculino</label><br/>
<label><input type="radio" name="sexo" value="feminino"/>Feminino</label><br/>

<label>Estado civil:</label><br />
<label><input type="radio" name="Estado_civil" value="Solteiro"/>Solteiro</label><br/>
<label><input type="radio" name="Estado_civil" value="Casado"/>Casado</label><br/>
<label><input type="radio" name="Estado_civil" value="Separado"/>Separado</label><br/>
<label><input type="radio" name="Estado_civil" value="Divorciado"/>Divorciado</label><br/>
<label><input type="radio" name="Estado_civil" value="Viuvo"/>Viuvo</label><br/>

<label>Insira uma foto de perfil:<br />
<label><input type="file" name="Foto"/></label><br />

<label> Email: <input type="text" name="Email" onchange="return validaEmail()"/></label>

</fieldset>

<fieldset>

<legend>Informações adicionais</legend>
Interesses:<br />
<label><input type="checkbox" name="Interesses" value="Filmes" />Filmes</label><br />
<label><input type="checkbox" name="Interesses" value="Musicas" />Musicas</label><br />
<label><input type="checkbox" name="Interesses" value="Séries" />Séries</label><br />
<label><input type="checkbox" name="Interesses" value="Animes" />Animes</label><br />
<label><input type="checkbox" name="Interesses" value="Livros" />Livros</label><br />
<label><input type="checkbox" name="Interesses" value="Cultura" />Cultura</label><br />

</fieldset>

<form action="">
<input type="submit" name="botao" value="Enviar"/>
<input type="reset" name="botao" value="Limpar"/>
<input type="button" name="botao" value="OK"/><br />

</body>

2
Dislike0
User badge image

Lucas Carvalho

vlw obg!!

1
Dislike0
User badge image

Alberto Yamamoto

o cód foi testado incremental, e no final parou de funcionar ?

acho uma boa comentar o JS inteiro, e ir separando por partes, até ver onde para de funcionar =)

testei de leve aqui fazendo teste  no cod e a parte de validar o form funciona até antes do var marcado = false, 

descomentando o cod do marcado o seu js para =)

funcionou bem

0
Dislike0

✏️ Responder

SetasNegritoItálicoSublinhadoTachadoCitaçãoCódigoLista numeradaLista com marcadoresSubscritoSobrescritoDiminuir recuoAumentar recuoCor da fonteCor de fundoAlinhamentoLimparInserir linkImagemFórmula

Para escrever sua resposta aqui, entre ou crie uma conta

User badge image

Outros materiais