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!

radio button validation 1

Status
Not open for further replies.

unreal

Programmer
Jun 28, 2001
8
0
0
CA
Hi there
I have a set of radio buttons that are generated dynamically,I never know how many sets of radio buttons will be generated, so i need to validate that one of the radio buttons of each set is checked.
I'm pretty new at javascript so if somebody could tell me what's wrong with this code I would apreciate very much
thanx
</body>

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
function validate(ivalue){
for(x = 0; x < ivalue.length; x++) {

if (document.status.ivalue[x].checked == false) {
alert(&quot;Error&quot;);
return false;
}
}

}
</SCRIPT>
<body>

<FORM ACTION=&quot;&quot; METHOD=&quot;post&quot; NAME=&quot;status&quot; ONSUBMIT=&quot;validate(this)&quot;><tr>
<input type=radio name='ship' value='SH| 22'>Shipped</td>
<input type=radio name='ship' value='BO| 22'>Back Order</td>
</tr>
<tr>
<input type=radio name='ship1' value='SH| 23'>Shipped</td>
<input type=radio name='ship1' value='BO| 23'>BackOrder</td>
</tr>
<tr>
<input type=radio name='ship2' value='SH| 24'>Shipped</td>
<input type=radio name='ship2' value='BO| 24'>BackOrder</td>
</tr>
</form>
 
couple questions: will there always be pairs?
will they always be named in consecutive numbers (ship1, ship2...)?
you need to make sure that one of EVERY pair is selected, right?

 
-yes, they will always be generated in pairs.
yes, they will always be generated in consecutive numbers and yes one of every pair has to be checked.
 
Ok: first, you need to add &quot;return&quot; before the call to your validation function in onsubmit, so your form waits for a return value.

This script will iterate through your form, and validate pairs of form elements named &quot;ship&quot; (with an optional number appended).

Code:
<html>
<head>
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
function validate() {
	var flag = new Array;
	var flagCount = 0;
	var count = 0;

	for(x = 0; x < document.forms[0].length; x++) {
		if (document.forms[0].elements[x].name.indexOf(&quot;ship&quot;) > -1) {
			if (x % 2 == 0) { // first of pair
				flag[flagCount] = false;

				if (document.forms[0].elements[x].checked) {
					flag[flagCount] = true;
				}
			}
			else { // second of pair
				if (document.forms[0].elements[x].checked) {
					flag[flagCount] = true;
				}
				flagCount++;
			}
		}

	}

	for (x = 0; x < flagCount; x++) {
		if (flag[x] == false) {
			count++;
		}
	}
	if (count > 0) {
		alert(&quot;You have left &quot; + count + &quot; pair(s) unchecked.&quot;);
		return false;
	}
}

</SCRIPT>

</head>
<body>

<FORM ACTION=&quot;&quot; METHOD=&quot;post&quot; NAME=&quot;status&quot; ONSUBMIT=&quot;return validate()&quot;>
<table>
<tr>
<input type=radio name='ship' value='SH| 22'>Shipped</td>
<input type=radio name='ship' value='BO| 22'>Back Order</td>
</tr>
<tr>
<input type=radio name='ship1' value='SH| 23'>Shipped</td>
<input type=radio name='ship1' value='BO| 23'>BackOrder</td>
</tr>
<tr>
<input type=radio name='ship2' value='SH| 24'>Shipped</td>
<input type=radio name='ship2' value='BO| 24'>BackOrder</td>
</tr>
<tr><input type=&quot;submit&quot; />
</tr>
</table>
</form>
</body>
</html>

Hope this helps!
 
Thanx a million it works perferct.
going to have to spend some time to understand it all but i'll get it

thanx again
aj
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top