form validation
Hello,
I have a question about validating form input. If I am using radio buttons to get some input, and I want to verify that a choice was made, I know I could use the following, for example:
if(document.someForm.year[0].checked==false &&
document.someForm.year[1].checked==false &&
document.someForm.year[2].checked==false &&
document.someForm.year[3].checked==false){
alert("Please select a year!");
return false;
}
However, it seems that there should be a better way. If there were, say 20 choices instead of just four, this would get very cumbersome. It seems you should be able to do something like:
if(document.someForm.year.checked==false)...
Something that would verify that an option was selected with one statment, rather than having to check each choice individually. Can this be done? Thanks for any help.
Bob
Mark Hensler posted this at 23:46 — 10th September 2000.
He has: 4,048 posts
Joined: Aug 2000
use a loop...
<script language=javascript>
<!--
function Validate() {
empty="yes"
// replace Number with the number of thingies
for (i=0; i < Number; i++) {
if (document.Form.thingy[i].checked==true) {
empty="no"
}
} //END for (i=0; i < Number; i++)
if(empty=="yes"){
alert("Please select a thingy!");
}
} //END function Validate()
//-->
</script>
Check the syntax and stuff, JavaScript isn't my strong point.
Mark Hensler
If there is no answer on Google, then there is no question.
Bob posted this at 23:54 — 10th September 2000.
They have: 117 posts
Joined: Feb 2000
Max,
Thanks for your reply! That's a good idea, and definitely is better than listing all the cases one by one. Should have thought of that myself! I still think they should have provided a statement like:
if(document.someForm.year.checked==false)
By leaving off the index it checks the whole list. Would have made things easier.
Thanks again!
Bob
Mark Hensler posted this at 05:08 — 11th September 2000.
He has: 4,048 posts
Joined: Aug 2000
A method like that would be a lot easier. An it'a be cleaner too. Just imagine a whole lott'a groups of checkboxes of radio buttons... If you had to check each group for one, that'a be a lot of loops.
Did it work?
Mark Hensler
If there is no answer on Google, then there is no question.
Bob posted this at 12:13 — 11th September 2000.
They have: 117 posts
Joined: Feb 2000
Max,
Yes, it worked. And I actually do have four seperate groups of radio buttons, with 6-8 choices per group. It was a real pain to verify, that's why I was looking for something easier! The loop is definitely easier than listing out 32 seperate "if" statements! Thanks again!
Bob
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.