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 strongm 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

Status
Not open for further replies.

hojoho

Technical User
Mar 8, 2003
44
US
I have a huge form(7 pages worth) that I am trying to validate. I have three pages of nothing but radio button and checkboxes. Some pages are mixed between the above stated and text fields. I am trying to make a single .js file that validates the majority of it. I am new to javascript so the document object model is not very fresh in my head. Here is my code.

for (var i = 0; i < form.elements.length - 1; i++){
if( form.elements
Code:
[i]
.checked || form.elements
Code:
[i]
.value != &quot;&quot; ){
continue;
}
else{
err[ a ] = form.elements
Code:
[i]
.name;
a += 1;
}
}

The form.elements
Code:
[i]
.checked is giving me the problems. I am not getting an error but my script is not acting the way I want it to. If anyone has any suggestions or can direct me in a different, more efficient way, please let me know.
Thanks ahead of time.
 
part of the problem is that your function does not recognize selection groups. for example, if a web page has three radio buttons all named &quot;color&quot;, with values of red, white and blue, and if a user selects red and then your function were to run, it would return &quot;selected&quot; for the radio button &quot;color&quot; with the value of red, but would then not return &quot;selected&quot; for the radio button &quot;color&quot; with the value of white. in other words, the above function will always mark the group in error unless all items are selected.

below is an example of your function modified to recognize groups...

Code:
<html>
<head>
<title>Untitled</title>
<script type=&quot;text/javascript&quot;>
<!--
function validate()
{
 for (var i = 0; i < document.forms[0].elements.length - 1; i++)
 {
  with (document.forms[0])
  {
   if (elements[i].type == &quot;radio&quot; || elements[i].type == &quot;checkbox&quot;)
	 {
	  if (!isChecked(eval(elements[i].name)))
	  {
	 	  alert (&quot;select box/button group failed validation!&quot;);
	  }
    i = i + eval(elements[i].name + &quot;.length - 1&quot;);
	 }
	 else if (elements[i].type == &quot;text&quot; && elements[i].value == &quot;&quot;)
	 {
	   alert (&quot;text box failed validation&quot;);
	 }
  } 
 }
}

function isChecked(obj)
{
 for (var i=0; i<obj.length; i++)
 {
   if (obj[i].checked)
   {
     return true;
     break;
   }
 }
 return false;   
}
// -->
</script>
</head>
<body>
<form name=&quot;frm&quot; method=&quot;post&quot;>
CB One: <input type=&quot;checkbox&quot; name=&quot;bx1&quot;><br>
CB Two: <input type=&quot;checkbox&quot; name=&quot;bx1&quot;>
<br><br>
RB One: <input type=&quot;radio&quot; name=&quot;bx2&quot;><br>
RB Two: <input type=&quot;radio&quot; name=&quot;bx2&quot;>
<br><br>
Text: <input type=&quot;text&quot; name=&quot;txt1&quot;>
<br><br>
<input type=&quot;button&quot; value=&quot;Run Function&quot; onclick=&quot;validate()&quot;>
</form>
</body>
</html>

hope that helps,

glenn

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top