Click to See Complete Forum and Search --> : Form validation and loops


Wildcat
11-07-2003, 04:23 PM
I'm trying to get a form validation script running, and it keeps giving me the rather unhelpful "syntax errors" as the only error message. The form is a simple choice of radio buttons and a textarea, and when I seperate the pieces the javascript runs pretty well. But when I put it together it all goes nuts. Any help will be appreciated.


var fav = false;
var com = false;

function valid() {
for(i=0; i<9; i++) {
if (document.vote.char[i].checked) {
fav = true; }
}
if (document.vote.why.value != "") {
com = true; }
if (fav == true) && (com == true) {
return true; }
else { alert("Please fill out the entire form");
return false; }
}

Charles
11-07-2003, 09:58 PM
Your syntax error is with this line, if_(fav_==_true)_&&_(com_==_true). It should be if_(fav_==_true_&&_com_==_true) or more simply if_(fav_&&_com). But your whole function is way more complicated than it needs to be. If you call it from the FORM element's start tag thusly, <form action="" onsubmit="return valid(this)"> then you could simply use

<script type="text/javascript">
<!--
function valid(form) {
var checked = false;
for (var i=0; i<9; i++) {if (form.char[i].checked checked = true}
if (!/\S/.test(form.why.value) || !checked) {alert('Please fill out the entire form.'); return false}
}
// -->
</script>

Wildcat
11-08-2003, 10:43 AM
Thanks; I had the feeling it was a bit dense. I'm trying to slog through a webdesign course that doesn't teach ANY Javascript but expects me to use it, so it's sometimes a bit difficult to see what's the best way to go about doing something.