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!

Works in IE, but not in FF or Netscape 1

Status
Not open for further replies.

Trusts

Programmer
Feb 23, 2005
268
US
Hi all,

Following is routine that performs some form validation. The routine is called in the form tag:

<form id="dls_form" method="post" action="enroll_method.php" onSubmit="return checkform(this)">

The routine works perfect in IE and is completely ignored in FF or Netscape. Any idea why? Here is the routine. Thanks!


function checkform(form1) {

var good_form = false;
var advanced = false;



for (var i=0;i<dls_form.elements["course_single"].length; i++){
//alert ("here");
if (dls_form.elements["course_single"].checked==true) {
good_form=true;
}
}
if (document.getElementById("course_multiple_50").checked==true) {
good_form=true;
advanced=true;
}
if (document.getElementById("course_multiple_51").checked==true) {
good_form=true;
advanced=true;
}
if (document.getElementById("course_multiple_52").checked==true) {
good_form=true;
advanced=true;
}
if (document.getElementById("course_multiple_53").checked==true) {
good_form=true;
advanced=true;
}
if (document.getElementById("course_multiple_54").checked==true) {
good_form=true;
advanced=true;
}
if (document.getElementById("course_multiple_55").checked==true) {
good_form=true;
advanced=true;
}
if (document.getElementById("course_multiple_56").checked==true) {
good_form=true;
advanced=true;
}

if (advanced==true && (dls_form.elements["payplan"][1].checked==true || dls_form.elements["payplan"][2].checked==true)) {
alert ("SLM and VA payment options apply only to courses 01-05");
return false;
}



if (!good_form==true) {
alert ("Must select at least one course");
return false;
}

if ((dls_form.elements["country"]!=="United States") && (dls_form.elements["payplan"][1].checked==true)) {
alert ("SLM Career Training Loans are only available for residents of the United States.");
return false;
}

return true;
}
</script>
 
Hi

Yep, only Explorer forgives all kind of... uhm... incorrect codes. Theoretically you are referring to the [tt]form[/tt] by [tt]id[/tt], but that is an Explorer-only mess, not a standard compliant object reference.

Anyway, your code loks abit random. You are passing the reference to the [tt]form[/tt] in parameter, but never use it.
Code:
function checkform(form1) {

var good_form = false;
var advanced =  false;

for (var i=0;i<[red]form1[/red].elements["course_single"].length; i++){
   if ([red]form1[/red].elements["course_single"][i].checked) {
      good_form=true;
   }
}
if (document.getElementById("course_multiple_50").checked) {
   good_form=true;
   advanced=true;
}   
if (document.getElementById("course_multiple_51").checked) {
   good_form=true;
   advanced=true;
}   
if (document.getElementById("course_multiple_52").checked) {
   good_form=true;
   advanced=true;
}   
if (document.getElementById("course_multiple_53").checked) {
   good_form=true;
   advanced=true;
}   
if (document.getElementById("course_multiple_54").checked) {
   good_form=true;
   advanced=true;
}   
if (document.getElementById("course_multiple_55").checked) {
   good_form=true;
   advanced=true;
}   
if (document.getElementById("course_multiple_56").checked) {
   good_form=true;
   advanced=true;
}

if  (advanced  &&  ([red]form1[/red].elements["payplan"][1].checked || [red]form1[/red].elements["payplan"][2].checked)) {
  alert ("SLM and VA payment options apply only to courses 01-05");
  return false;
}
  


if (!good_form) {
 alert ("Must select at least one course");
  return false;
 }

if  (([red]form1[/red].elements["country"]!=="United States")  &&  ([red]form1[/red].elements["payplan"][1].checked)) {
  alert ("SLM Career Training Loans are only available for residents of the United States.");
  return false;
}

return true;
}
By the way, when you check a boolean value in an [tt]if[/tt] condition, is pointless to compare it with another boolean value just to get another boolean value.

Feherke.
 
Thanks feherke, got it to work! Feel like taking a look at another one?

I have a set of radio buttons and a set of checkboxes. When any radio button is clicked, all checkboxes are to be cleared. When any checkbox is clicked, all radio buttons should be clear.

The name of the radio button group is course_single. The checkboxes are course_multiple_50, course_multiple_51, etc.

The routine works in IE but does not work in FF (actually this is the routine I meant to ask about in the first place! Need more coffee!)

The clicks are caught like this:
<input name="course_multiple_54" id="course_multiple_54" type="checkbox" value="54" onClick="update_selection_1()" />

<input name="course_single" type="radio" value="02" onClick="update_selection_2()" />

And here is the javascript:


<Script language="JavaScript" type="text/javascript">
function update_selection_1() {
for (var i=0;i<dls_form.elements["course_single"].length; i++){
dls_form.elements["course_single"].checked=false;
}
}
</script>

<Script language="JavaScript" type="text/javascript">
function update_selection_2() {
document.getElementById("course_multiple_50").checked=false;
document.getElementById("course_multiple_51").checked=false;
document.getElementById("course_multiple_52").checked=false;
document.getElementById("course_multiple_53").checked=false;
document.getElementById("course_multiple_54").checked=false;
document.getElementById("course_multiple_55").checked=false;
document.getElementById("course_multiple_56").checked=false;
}
</script>


Thanks!
 
Hi

While there you have no parameter, I think the simplest would be like this :
Code:
<script language="JavaScript" type="text/javascript">
function update_selection_1()
{
  [red]var dls_form=document.getElementById('dls_form')[/red]
  for (var i=0;i<dls_form.elements["course_single"].length; i++){
    dls_form.elements["course_single"][i].checked=false;
  }
}

function update_selection_2()
{
  [red]for (var i=50;i<=56;i++)[/red]
    document.getElementById("course_multiple_"[red]+i[/red]).checked=false;
}
</script>
And while the checkboxes are numbered sequentially, I would take advantage of that withe a [tt]for[/tt].

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top