I want to know what's wrong with this code (I'm an amateur programmer).
I'm trying to check if every field has a value or if checkboxes/radios
have at least one item checked on each group (yes, you know, with the
same name in the HTML tag).
I want to have a generic code which I can fit to every document.
The problem is that it never got into the line:
if(elm.type == "checkbox" || elm.type == "radio")
I've put an alert(elm.type)
function checkallfields() {
var anychecked = true;
for(f = 0; f < document.forms.length ; f++) {
frm = document.forms[f];
for(e = 0; e < frm.elements.length; e++) {
elm = frm.elements[e];
if(elm.type == "checkbox" || elm.type == "radio") {
var passcheck = false;
for(c = 0; c < frm[elm.name].lenght; c++) {
n = e + c;
passcheck = passcheck || frm.element[n].checked;
}
e += c
anychecked = anychecked && passcheck;
} else {
if(elm.value == "") anychecked = anychecked && false;
}
}
}
return anychecked;
}
Well, here's the form which I'm doing the tests with:
<form method="POST" action="process.php"
onsubmit="return checkallfields()">
<input type="hidden" name="cat" value="1">
e-mail:<input type="text" name="email_respuesta" size="20">
<br>textarea:<textarea rows="2" name="textarea1" cols="20"></textarea>
<br>checkboxes:<input type="checkbox" name="checkboxes1" value="A">
<input type="checkbox" name="checkboxes1" value="B">
<br>radios:<input type="radio" value="V1" checked name="radios1">
<input type="radio" name="radios1" value="V2">
<br><input type="submit" value="Send">
<input type="reset" value="Clear">
<input type="button" value="check" onclick="alert(checkallfields());">
</form>
Thank you very much!