quando più elementi di un form hanno lo stesso nome formano una collezione.
devi controllare la collezione quando il campo si chiama "auto" e dare a un solo radio la classe "si"
codice:
function check_valid(theForm) {
var autoSelezionato
for (i=0; i < theForm.elements.length; i++) {
if (theForm.elements[i].value == "" && theForm.elements[i].className == "si") {
alert (theForm.elements[i].name + ": campo vuoto non accettabile");
theForm.elements[i].value = "";
theForm.elements[i].focus();
return false;
}
if (theForm.elements[i].className == "si") {
if (theForm.elements[i].name == "immagine_auto") { // radio
if (theForm.elements[i].checked == false) {
alert(theForm.elements[i].name + ": campo non spuntato");
// theForm.elements[i].value = "";
theForm.elements[i].focus();
return false;
}
}
if (theForm.elements[i].name == "privacy") { // checkbox
if (theForm.elements[i].checked == false) {
alert(theForm.elements[i].name + ": campo non spuntato");
// theForm.elements[i].value = "";
theForm.elements[i].focus();
return false;
}
}
if (theForm.elements[i].name == "username") { // text
var errato="";
errato = verify_username(theForm.elements[i].value);
if (errato != "") {
alert ("Per " + theForm.elements[i].name + " " + errato);
theForm.elements[i].value = "";
theForm.elements[i].focus();
return false;
}
}
if (theForm.elements[i].name == "auto") {
el=theForm.auto;
autoSelezionato=false;
for(j=0;j<el.length;j++){
if(el[j].checked)autoSelezionato=true;
}
if(!autoSelezionato){
alert(theForm.elements[i].name + ": campo non spuntato");
theForm.elements[i].focus();
return false;
}
}
// ============== continua elenco dei controlli da controllare ==============
}
}
}
codice:
<form id="form2" name="form2" method="post" action="register_post.php" onsubmit="return check_valid(this);">
<input type="radio" name="auto" value="auto_1.png" class="si" />
<input type="radio" name="auto" value="auto_2.png" />
<input type="radio" name="auto" value="auto_3.png" />
......................
<input name="priorita" size="20" type="text" class="si" />
<input name="Registra" value="Registra" type="submit" />
</form>