Generel form validator
Jeg har lavet den her validator der virker med en enkelt radio... Er der en der har en optimal løsning på check af flere radios og gerne samtidig checkboxe således vil alle har en generel form validator vi kan bruge ?---
function validateForm (form) {
var radio = \"\";
var radiochecked = false;
for (i = 0; i < document.form.elements.length; i++) {
var which = document.form.elements[i];
var alertMsg = \"Alle felter *skal* udfyldes\\n\";
var sizeAlertMsg = \"Et passwordfelt skal indeholde mindst 6 karakterer\\n\";
if (which.type == \"text\") {
if (which.value == \"\") {
alert(alertMsg + \"> \" + which.name);
which.focus();
return false;
}
} else if (which.type == \"password\") {
if (which.value == \"\") {
alert(alertMsg + \"> \" + which.name);
which.focus();
return false;
}
if (which.value.length < 6) {
alert(sizeAlertMsg + \"> \" + which.name);
which.focus();
return false;
}
} else if (which.type == \"textarea\") {
if (which.value == \"\") {
alert(alertMsg + \"> \" + which.name);
which.focus();
return false;
}
} else if (which.type == \"select-one\") {
if (which.options.selectedIndex == -1) {
alert(alertMsg + \"> \" + which.name);
which.focus();
return false;
}
if (which.options.value == -1) {
alert(alertMsg + \"> \" + which.name);
which.focus();
return false;
}
} else if (which.type == \"select-multiple\") {
if (which.options.selectedIndex == -1) {
alert(alertMsg + \"> \" + which.name);
which.focus();
return false;
}
}
if (which.type == \"radio\") {
radio = which.name;
if (which.checked == true) {
radiochecked = true;
}
}
}
if (radiochecked == false) {
alert(alertMsg + \"> \" + radio);
which.focus();
return false;
}
return true;
}
