Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

form validation

Status
Not open for further replies.

arcturis

IS-IT--Management
Sep 2, 2002
15
0
0
CA
I though this was going to be simple, but...

Anyways, here's the problem, I've got a simple form, just one group of radio buttons (for a voting poll), that i need to have some validation (at least one need to be selected) then submit the form to a pop-up, where my vbscript does a database insert then draws a bar graph.

I can ge the form to submit to the pop-up, but not validate before hand. Any help woul dbe great.

Thanks
 
see thread216-537307 and check if any of the option button is checked by using a loop
 
arcturis, is this what you were looking for??

<html>
<head>

<script>
function YourChoice() {
if(!document.form1.choice[0].checked && !document.form1.choice[1].checked &&!document.form1.choice[2].checked)
{
alert(&quot;please choose one&quot;);
return false;
}
return true;
}
</script>
</head>

<body bgcolor=&quot;#FFFFFF&quot; text=&quot;#000000&quot;>
<form name=&quot;form1&quot; method=&quot;post&quot; action=&quot;mailto:mmm@aol.com&quot; onSubmit=&quot;return YourChoice();&quot;>
<input type=&quot;radio&quot; name=&quot;choice&quot; value=&quot;1&quot;>
<input type=&quot;radio&quot; name=&quot;choice&quot; value=&quot;2&quot;>
<input type=&quot;radio&quot; name=&quot;choice&quot; value=&quot;3&quot;>

<input type=&quot;submit&quot; name=&quot;Submit&quot; value=&quot;Submit&quot;>
</form>
</body>
</html>


The above will throw an alert if no radio button is selected...

Forget the Nobel Peace prize, I just want to take over the world!! [hammer]
 
GUJUm0del, thanks for the reply, but this statement

Code:
if(!document.form1.choice[0].checked && !document.form1.choice[1].checked &&!document.form1.choice[2].checked)

would get hard to manage if there are more than three radio buttons. By nature radio button work in groups, so either one is checked, or none. Is there an easy to do this? or am i outta luck?

Arcturis
 
Do a for loop:

var checkedpos=-1;
for (var i=0; i < 3; i++) {
if (document.form1.choice.checked) {
checkedpos = i;
break;
}
}
if (checkedpos == -1)
alert(&quot;Please check a thingy&quot;);
 
That's:

var checkedpos=-1;
for (var i=0; i < 3; i++) {
if (document.form1.choice.checked) {
checkedpos = i;
break;
}
}
if (checkedpos == -1)
alert(&quot;Please check a thingy&quot;);


Sorry, I hate this TGML stuff...its very bad for program code, especially when I like to use &quot;i&quot; as a counter.
 
Looks like Pimonkey got it before me, lol...
Yeah, you can use loops for the same effect...

Forget the Nobel Peace prize, I just want to take over the world!! [hammer]
 
Thanks guys, now on to finishing this voting poll. Next time i say &quot;sure, that's easy&quot;, i'll kick my self.

Arc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top