VALIDAÇÃO DE RADIO BUTTON EM FORMULÁRIOS
quarta-feira, 2 de julho de 2008 | Marcadores: javascript | |Inclua o código abaixo entre a tag <head></head>
<script>
function checa(nform) {
//validacao de radio buttons sem saber quantos sao
marcado = -1
for (i=0; i<nform.resp.length; i++) {
if (nform.resp[i].checked) {
marcado = i
resposta = nform.resp[i].value
}
}
if (marcado == -1) {
alert("Selecione uma resposta.");
nform.resp[0].focus();
return false;
}
return true;
}
</script>
Para ver o funcionamento, basta não clicar em nenhuma das opções abaixo:
<form action="validacao_radio_button.asp" method="post" name="form1" onSubmit="return checa(this);">
Você é?<br>
<input type="radio" name="resp" value="Homem">Homem <input type="radio" name="resp" value="Mulher">Mulher
<input type="submit" name="responde" value="Responder"> </form>