Ir para conteúdo
Fórum Script Brasil
  • 0

(Resolvido) Validando formulário


luis Romao

Pergunta

Bom dia pessoal.

Esse é o meu primeiro post aqui e gostaria de ajuda.

Eu tenho um formulário e gostaria de validar um campo textbox quando uma das opções de radio selecionadas.

Segue o código

<html>
<head>
<title>Validando</title>
</head>
<body>
<form id="form1" name="form1" method="post" action=">
  <p>
    <label>
<input type="radio" name="validador" value="1" id="validador_0" />
Não validado</label>
    <br />
    <label>
      <input type="radio" name="validador" value="2" id="validador_1" />
      Validar o campo de texto</label>
  </p>
  <p>
    <label>
      <input type="text" name="txtnome" id="txtnome" />
    </label>
  </p>
  <p>
    <label>
      <input type="submit" name="button" id="button" value="Enviar" />
    </label>
    <br />
  </p>
</form>
</body>
</html>

Link para o comentário
Compartilhar em outros sites

7 respostass a esta questão

Posts Recomendados

  • 0

faz um teste ae

function valida(){
    var rd = document.getElementsByName('validador');
    var txt = document.getElementById('txtnome');
    var vl = 0;
    for(i=0; i < rd.length; i++ ){
         if (rd[i].checked == true) vl = rd[i].value;
    }

    switch (vl){
        case '1':
            return true;
        case '2':
            if (txt.value == '' || txt.value == 'undefined'){
                alert('campo obrigatório');
                return false;
            }else{
                return true;
            }
        default:
            alert('selecione uma opção');
            return false;
    }
}
<form id="form1" name="form1" method="post" onSubmit="java script: return valida();">
    <p>
        <label><input type="radio" name="validador" value="1" id="validador_0" />Não validado</label>
        <br />
        <label><input type="radio" name="validador" value="2" id="validador_1" />Validar o campo de texto</label>
    </p>
    <p>
        <label><input type="text" name="txtnome" id="txtnome" /></label>
    </p>
    <p>
        <label><input type="submit" name="button" id="button" value="Enviar" /></label>
        <br />
    </p>
</form>

Link para o comentário
Compartilhar em outros sites

  • 0

Cara, muito obrigado por ter respondido, mas não funcionou.

eu fiz um outro formulário.

Ele está validando, mas sem as marcações acima.

Olha ele ae.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>validar</title>
<script language="JavaScript">
function validar(){
if (document.form1.txtnome.value=="")
{
alert( "Preencha o campo MENSAGEM!" );
document.form1.txtnome.focus();
}
return false;


if (document.form1.radio.value=="")
{
alert( "Marque a opção acima!" );
document.form1.radio.focus();
}
return false;
}
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="" onsubmit="return validar();">
  <label>
    <input type="radio" name="radio" id="radio" value="1" />
    Se marcado é necessário preencher o campo abaixo.
    <br>
    <input type="radio" name="radio" id="radio2" value="2">
    Se marcado não é necessário preencher o campo texto.<br>
    <br />
  </label>
  <input type="text" name="txtnome" id="txtnome" />
  <input type="submit" name="button" id="button" value="Enviar" />
</form>
</body>
</html>

Obrigado pela ajuda até o momento.

Link para o comentário
Compartilhar em outros sites

  • 0

O código anterior estava errado

considerar esse abaixo

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>validar</title>
<script language="JavaScript">
function validar(){
if (document.form1.txtnome.value=="")
{
alert( "Preencha o campo MENSAGEM!" );
document.form1.txtnome.focus();
}
return false;
}
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="" onSubmit="return validar();">
  <label>
    <input type="radio" name="radio" id="radio" value="1" />
    Se marcado é necessário preencher o campo abaixo.
    <br>
    <input type="radio" name="radio" id="radio2" value="2">
    Se marcado não é necessário preencher o campo texto.<br>
    <br />
  </label>
  <input type="text" name="txtnome" id="txtnome" />
  <input type="submit" name="button" id="button" value="Enviar" />
</form>
</body>
</html>

Link para o comentário
Compartilhar em outros sites

  • 0

Consegui fazer o que precisava!!!!

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>validar</title>
<script>
function ValidaFormulario(){
    if ( document.form1.radio[1].checked )

       if (document.form1.txtnome.value=="")
{
alert( "Preencha o campo MENSAGEM!" );
document.form1.txtnome.focus();
}
return false;
}

</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="" onSubmit="return ValidaFormulario();">
  <label>
    <input type="radio" name="radio" id="radio1" value="1"/>
    Se marcado é necessário preencher o campo abaixo.
    <br>
    <input type="radio" name="radio" id="radio2" value="2">
    Se marcado não é necessário preencher o campo texto.<br>
    <br />
  </label>
  <input type="text" name="txtnome" id="txtnome" />
  <input type="submit" name="button" id="button" value="Enviar" />
</form>
</body>
</html>

Link para o comentário
Compartilhar em outros sites

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152k
    • Posts
      651,8k
×
×
  • Criar Novo...