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!

Javascript not working in Mozilla

Status
Not open for further replies.

steve100

Technical User
Aug 15, 2002
35
0
0
GB
Hi I have a simple form validation script which works fine in IE 6.0 and Opera 7.0. However it doesn't do anything in Mozilla 1.4 or Mozilla Firebird and I am not sure why.

I link to the script using an onsubmit event handler in the form tag:

<form method=&quot;post&quot; action=&quot;someform.cgi&quot; id=&quot;idname&quot; onsubmit=&quot;return verify(this);&quot; >

Within the head tags a script tag defines the location of the js file.

<script language=&quot;javascript&quot; type=&quot;text/javascript&quot; src=&quot;../scripts/somefile.js&quot;></script>

Within the js file the script looks like this:


// This is the function that performs form verification. It is invoked
// from the onsubmit event handler. The handler should return whatever
// value this function returns.
function verify(f) {

var fname = -1;
var lname = -1;
var address = -1;
var postcode = -1;
var telephone = -1;
var region = -1;
var gender = -1;
var age = -1;
var faculty = -1;
var interest = -1;
var category = -1;
var experience = -1;
var membership = -1;
var contact = -1;
var issues = -1;

var msg = &quot; &quot;;
var empty_fields = &quot;&quot;;


/* Verify first name is entered */

if (feedbackform.aafname.value != &quot;&quot;) {
fname = 1;
}

if (fname == -1) {
empty_fields += &quot;first name&quot; + &quot;\n&quot;;
}

/* Verify last name is entered */

if (feedbackform.bblname.value != &quot;&quot;) {
lname = 1;
}

if (lname == -1) {
empty_fields += &quot;last name&quot; + &quot;\n&quot;;
}

/* Verify address has been entered */

if (feedbackform.ccaddress.value != &quot;&quot;) {
address = 1;
}

if (address == -1) {
empty_fields += &quot;address&quot; + &quot;\n&quot;;
}


/* Verify postcode has been entered */

if (feedbackform.ddpostcode.value != &quot;&quot;) {
postcode = 1;
}

if (postcode == -1) {
empty_fields += &quot;postcode&quot; + &quot;\n&quot;;
}

/* Verify preferred telephone contact number has been entered */

if (feedbackform.ffphone.value != &quot;&quot;) {
telephone = 1;
}

if (telephone == -1) {
empty_fields += &quot;telephone&quot; + &quot;\n&quot;;
}

/* Verify region has been entered */

if (feedbackform.ggregion.value != &quot;&quot;) {
region = 1;
}

if (region == -1) {
empty_fields += &quot;region&quot; + &quot;\n&quot;;
}


/* Verify gender has been entered */

for (i=0; i<feedbackform.hhgender.length; i++) {
if (feedbackform.hhgender.checked) {
gender = 1;
}
}
if (gender == -1) {
empty_fields += &quot;gender&quot; + &quot;\n&quot;;
}

/* Verify age has been entered */

for (i=0; i<feedbackform.iiage.length; i++) {
if (feedbackform.iiage.checked) {
age = 1;
}
}
if (age == -1) {
empty_fields += &quot;age&quot; + &quot;\n&quot;;
}


/* Verify faculty has been entered */

if (feedbackform.jjfaculty.value != &quot;&quot;) {
faculty = 1;
}

if (faculty == -1) {
empty_fields += &quot;faculty&quot; + &quot;\n&quot;;
}


/* Verify interest has been entered */

if (feedbackform.kkinterest.value != &quot;&quot;) {
interest = 1;
}

if (interest == -1) {
empty_fields += &quot;main area of interest&quot; + &quot;\n&quot;;
}


/* Verify category has been entered */

if (feedbackform.lljobcategory.value != &quot;&quot;) {
category = 1;
}

if (category == -1) {
empty_fields += &quot;job category&quot; + &quot;\n&quot;;
}


/* Verify 'summary of experience' has been entered */

if (feedbackform.mmsummaryofexperience.value != &quot;&quot;) {
experience = 1;
}

if (experience == -1) {
empty_fields += &quot;summary of experience&quot; + &quot;\n&quot;;
}


/* Verify 'professional membership' has been entered */

if (feedbackform.nnprofessionalmembership.value != &quot;&quot;) {
membership = 1;
}

if (membership == -1) {
empty_fields += &quot;membership of professional bodies&quot; + &quot;\n&quot;;
}


/* Verify 'how contacted' has been entered */

for (i=0; i<feedbackform.oohowcontactedbyseeker.length; i++) {
if (feedbackform.oohowcontactedbyseeker.checked) {
contact = 1;
}
}
if (contact == -1) {
empty_fields += &quot;preference for contact by career seeker&quot; + &quot;\n&quot;;
}

/* Verify 'issues to talk about' has been entered */

for (i=0; i<feedbackform.qqissuestotalkabout.length; i++) {
if (feedbackform.qqissuestotalkabout.checked) {
issues = 1;
}
}
if (issues == -1) {
empty_fields += &quot;issues to talk about&quot; + &quot;\n&quot;;
}





/* Check if any required fields are empty, if they are give an error message with the field name */

if (empty_fields !=&quot;&quot;) {
msg +=&quot;______________________________________________________\n\n&quot;
msg += &quot;The form was not submitted because of the following error(s).\n&quot;;
msg += &quot;- The following required field(s) are empty:&quot; + &quot;\n&quot;
+empty_fields + &quot;\n&quot;;
alert(msg);

/* Don't submit the form */
return false;

}

else {
/* Submit the form */
return true;
}


}


Does anyone know what the problem is - have I not used the correct DOM reference for Mozilla?

Thanks
Steve
 
Can you repost your JS, but this time wrap it in
Code:
[
Code:
code]
tags? Not doing so means we lose a lot of the meaning of the source - as you can no doubt see.

Can you also post the complete HTML?

Thanks,

Dan
 
My complete HTML form

<form action=&quot;someform.cgi&quot; method=&quot;post&quot; name=&quot;feedbackform&quot; id=&quot;feedbackform&quot; onsubmit=&quot;return verify(this);&quot;>

<input type=&quot;hidden&quot; name=&quot;mailto&quot; value=&quot;someaddress&quot;/>
<input type=&quot;hidden&quot; name=&quot;mmsubj&quot; value=&quot;Title- seeker form submission&quot;/>
<input type=&quot;hidden&quot; name=&quot;npage&quot; value=&quot;someurl&quot;/>

<table>
<tr>
<td class=&quot;lbl&quot; id=&quot;left&quot;><label for=&quot;fname&quot;>First name *</label></td>
<td id=&quot;right&quot;><input name=&quot;aafname&quot; type=&quot;text&quot; id=&quot;fname&quot;/></td>
</tr>
<tr>
<td class=&quot;lbl&quot;><label for=&quot;lname&quot;>Family name *</label></td>
<td><input type=&quot;text&quot; id=&quot;lname&quot; name=&quot;bblname&quot;/></td>
</tr>
<tr>
<td class=&quot;lbl&quot;><label for=&quot;address&quot;>Address *</label></td>
<td><input type=&quot;text&quot; id=&quot;address&quot; name=&quot;ccaddress&quot;/></td>
</tr>
<tr>
<td class=&quot;lbl&quot;><label for=&quot;postcode&quot;>Postcode *</label></td>
<td><input type=&quot;text&quot; id=&quot;postcode&quot; name=&quot;ddpostcode&quot;/></td>
</tr>
<tr>
<td class=&quot;lbl&quot;><label for=&quot;email&quot;>E-mail </label></td>
<td><input type=&quot;text&quot; id=&quot;email&quot; name=&quot;ee_email&quot;/></td>
</tr>
<tr>
<td class=&quot;lbl&quot;><label for=&quot;phone&quot;>Preferred telephone contact number
*</label></td>
<td>
<ul>
<li>
<input type=&quot;text&quot; id=&quot;phone&quot; name=&quot;ffphone&quot;/>
</li>
<li>
<input name=&quot;ffpreferredcontact&quot; id=&quot;daytime&quot; type=&quot;checkbox&quot; value=&quot;daytimenumber&quot; />
<label for=&quot;daytime&quot;>Day time number</label>
</li>
<li>
<input name=&quot;ffpreferredcontact&quot; id=&quot;evening&quot; type=&quot;checkbox&quot; value=&quot;eveningnumber&quot; />
<label for=&quot;evening&quot;>Evening number</label>
</li>
</ul></td>
</tr>
<tr>
<td class=&quot;lbl&quot;><label for=&quot;pinumber&quot;>Personal Indicator (PI) number * </label></td>
<td><input type=&quot;text&quot; id=&quot;pinumber&quot; name=&quot;zzpinumber&quot; /></td>
</tr>
<tr>
<td class=&quot;lbl&quot;><label for=&quot;region&quot;>Region *</label></td>
<td><select name=&quot;ggregion&quot; id=&quot;region&quot;>
<option selected=&quot;selected&quot;>--Please select--</option>
<option value=&quot;region1&quot;>Region 1</option>
<option value=&quot;region2&quot;>Region 2</option>
<option value=&quot;region3&quot;>Region 3</option>
<option value=&quot;region4&quot;>Region 4</option>
<option value=&quot;region5&quot;>Region 5</option>
<option value=&quot;region6&quot;>Region 6</option>
<option value=&quot;region7&quot;>Region 7</option>
<option value=&quot;region8&quot;>Region 8</option>
<option value=&quot;region9&quot;>Region 9</option>
<option value=&quot;region10&quot;>Region 10</option>
<option value=&quot;region11&quot;>Region 11</option>
<option value=&quot;region12&quot;>Region 12</option>
<option value=&quot;region13&quot;>Region 13</option>
</select></td>
</tr>
<tr>
<td class=&quot;lbl&quot;>Gender *</td>
<td>
<ul>
<li>
<input name=&quot;hhgender&quot; id=&quot;male&quot; type=&quot;radio&quot; value=&quot;male&quot; /><label for=&quot;male&quot;>Male</label>
</li>
<li>
<input name=&quot;hhgender&quot; id=&quot;female&quot; type=&quot;radio&quot; value=&quot;female&quot; /><label for=&quot;female&quot;>Female</label>
</li>
</ul>
</td>
</tr>
<tr>
<td class=&quot;lbl&quot;>Please indicate your age *</td>
<td>
<ul>
<li>
<input name=&quot;iiage&quot; id=&quot;under_25&quot; type=&quot;radio&quot; value=&quot;under_25&quot; /><label for=&quot;under_25&quot;>Under 25</label>
</li>
<li>
<input name=&quot;iiage&quot; id=&quot;_25_39&quot; type=&quot;radio&quot; value=&quot;25_39&quot; /><label for=&quot;_25_39&quot;>25 - 39</label>
</li>
<li>
<input name=&quot;iiage&quot; id=&quot;_40_49&quot; type=&quot;radio&quot; value=&quot;40_49&quot; /><label for=&quot;_40_49&quot;>40 - 49</label>
</li>
<li>
<input name=&quot;iiage&quot; id=&quot;_50plus&quot; type=&quot;radio&quot; value=&quot;50+&quot; /><label for=&quot;_50plus&quot;>50+</label>
</li>
</ul></td>
</tr>
<tr>
<td class=&quot;lbl&quot;><label for=&quot;faculty&quot;>In which OU faculty have you studied the most? (Please indicate) *</label></td>
<td>
<select name=&quot;jjfaculty&quot; id=&quot;faculty&quot;>
<option selected=&quot;selected&quot;>--Please select--</option>
<option value=&quot;arts&quot;>Arts</option>
<option value=&quot;languagestudies&quot;>Education and Language Studies</option>
<option value=&quot;health&quot;>Health and Social Welfare</option>
<option value=&quot;maths&quot;>Maths and Computing</option>
<option value=&quot;business&quot;>OU Business School</option>
<option value=&quot;science&quot;>Science</option>
<option value=&quot;socialscience&quot;>Social Science</option>
<option value=&quot;technology&quot;>Technology</option>
</select>
</td>
</tr>
<tr>
<td class=&quot;lbl&quot;>
<p>
<label for=&quot;interest&quot;>Main career area of interest *</label>
</p>
<p>
<label for=&quot;interest&quot;>Please be specific as this will help us to make
the most suitable links for you (e.g. Systems Analysis rather than simply
IT). If you are interested in teaching, please give level and subject
(e.g. Secondary school history).</label>
</p></td>
<td><input name=&quot;kkinterest&quot; type=&quot;text&quot; id=&quot;interest&quot; size=&quot;40&quot; /></td>
</tr>
<tr>
<td class=&quot;lbl&quot;><label for=&quot;jobcategory&quot;>Please select the most appropriate job category that relates to the main career area you are interested in. *</label></td>
<td><select name=&quot;lljobcategory&quot; id=&quot;jobcategory&quot;>
<option value=&quot;Administration&quot;>Administration</option>
<option value=&quot;Advertising&quot;>Advertising, Promotion and Marketing</option>
<option value=&quot;Animal&quot;>Animal and Plant Resources</option>
<option value=&quot;Buying&quot;>Buying, Selling and Retailing</option>
<option value=&quot;Creative_Arts&quot;>Creative Arts, Design and Crafts</option>
<option value=&quot;Education&quot;>Education</option>
<option value=&quot;Employment_Labour&quot;>Employment and Labour</option>
<option value=&quot;Engineering&quot;>Engineering</option>
<option value=&quot;Finance&quot;>Finance</option>
<option value=&quot;Health_Care&quot;>Health Care</option>
<option value=&quot;Hotel, Catering and Hotel&quot;>Hotel, Catering and Accommodation
Services</option>
<option value=&quot;Information_Services&quot;>Information Services</option>
<option value=&quot;Information_Technology&quot;>Information Technology and
Management Services</option>
<option value=&quot;Insurance_Pensions&quot;>Insurance and Pensions</option>
<option value=&quot;Law_Enforcement&quot;>Law Enforcement and Protection</option>
<option value=&quot;Legal_Services&quot;>Legal Services</option>
<option value=&quot;Leisure_Recreation&quot;>Leisure and Recreation Services</option>
<option value=&quot;Manufacturing_Processing&quot;>Manufacturing and Processing</option>
<option value=&quot;Physical_Resources&quot;>Physical Resources and the Environment</option>
<option value=&quot;Property_Construction&quot;>Property and Construction</option>
<option value=&quot;Publishing&quot;>Publishing, Media and Performing Arts</option>
<option value=&quot;Scientific_Services&quot;>Scientific Services</option>
<option value=&quot;Social_Pastoral_Care&quot;>Social and Pastoral Care</option>
<option value=&quot;Transport_Distribution]&quot;>Transport and Distribution</option>
<option selected=&quot;selected&quot;>--Please select--</option>
</select></td>
</tr>
<tr>
<td class=&quot;lbl&quot;>Please indicate the issues you would like to talk about.
(You may select more than one).*</td>
<td>
<ul>
<li>
<input name=&quot;mmissuestotalkabout&quot; id=&quot;jobcontent&quot; type=&quot;checkbox&quot; value=&quot;jobcontent&quot; /><label>Job content</label>
</li>
<li>
<input name=&quot;mmissuestotalkabout&quot; id=&quot;howtofindajob&quot; type=&quot;checkbox&quot; value=&quot;howtofindajob&quot; /><label for=&quot;howtofindajob&quot;>How to find a job</label>
</li>
<li>
<input name=&quot;mmissuestotalkabout&quot; id=&quot;promotion&quot; type=&quot;checkbox&quot; value=&quot;promotion&quot; /><label for=&quot;promotion&quot;>Promotion (how to progress)</label>
</li>
<li>
<input name=&quot;mmissuestotalkabout&quot; id=&quot;selfemployment&quot; type=&quot;checkbox&quot; value=&quot;selfemployment&quot; /><label for=&quot;selfemployment&quot;>Self-employment</label>
</li>
<li>
<input name=&quot;mmissuestotalkabout&quot; id=&quot;training&quot; type=&quot;checkbox&quot; value=&quot;training&quot; /><label for=&quot;training&quot;>Training</label>
</li>
<li>
<input name=&quot;mmissuestotalkabout&quot; id=&quot;volunteering&quot; type=&quot;checkbox&quot; value=&quot;volunteering&quot; /><label for=&quot;volunteering&quot;>Volunteering</label>
</li>
<li>
<input name=&quot;mmissuestotalkabout&quot; id=&quot;shadowing&quot; type=&quot;checkbox&quot; value=&quot;shadowing&quot; /><label for=&quot;shadowing&quot;>Work shadowing/experience</label>
</li>
<li><input type=&quot;checkbox&quot; id=&quot;otherchk&quot; name=&quot;qqissuestotalkabout&quot; value=&quot;other&quot; /><label for=&quot;other&quot;>Other (please describe)</label><input name=&quot;mmissuestotalkabout&quot; type=&quot;text&quot; id=&quot;other&quot; size=&quot;40&quot; /></li>
</ul>
</td>
</tr>
<tr>
<td></td>
<td><input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;submit&quot;/></td>
</tr>
</table>
</form>











 

>> Can you repost your JS, but this time wrap it in
Code:
[
Code:
code]
tags?

Can you please post your JS code, as I asked, using
Code:
[
Code:
code]
tags to surround it? Not doing so means we lose bits of the code.

I don't ask these things for no reason, and having to ask twice is very annoying.

Dan
 
Code:
function verify(f) {

   var fname = -1;
   var lname = -1;
   var address = -1;
   var postcode = -1;
   var telephone = -1;
   var pinumber = -1;
   var region = -1;   
   var gender = -1;
   var age = -1;
   var faculty = -1;
   var interest = -1;
   var category = -1; 
   var issues = -1; 
   
   var msg = &quot; &quot;;
   var empty_fields = &quot;&quot;;
  	
  
   /* Verify first name is entered */
  
   if (feedbackform.aafname.value != &quot;&quot;) {
       fname = 1;
       }
      
   if (fname == -1) {
	  empty_fields += &quot;first name&quot; + &quot;\n&quot;;     
	  }
	
	/* Verify last name is entered */
	
   if (feedbackform.bblname.value != &quot;&quot;) {
       lname = 1;
       }
      
   if (lname == -1) {
	 empty_fields += &quot;last name&quot; + &quot;\n&quot;;     
	 }
	
   /* Verify address has been entered */
   
   if (feedbackform.ccaddress.value != &quot;&quot;) {
       address = 1;
       }
      
   if (address == -1) {
	  empty_fields += &quot;address&quot; + &quot;\n&quot;;     
	  }
   
   
   /* Verify postcode has been entered */
   
   if (feedbackform.ddpostcode.value != &quot;&quot;) {
       postcode = 1;
       }
      
   if (postcode == -1) {
	  empty_fields += &quot;postcode&quot; + &quot;\n&quot;;     
	  }
   
   /* Verify preferred telephone contact number has been entered */
   
   if (feedbackform.ffphone.value != &quot;&quot;) {
       telephone = 1;
       }
      
   if (telephone == -1) {
	  empty_fields += &quot;telephone&quot; + &quot;\n&quot;;     
	  }
	  
  
  /* Verify preferred pinumber contact number has been entered */
   
   if (feedbackform.zzpinumber.value != &quot;&quot;) {
       pinumber = 1;
       }
      
   if (pinumber == -1) {
	  empty_fields += &quot;pi number&quot; + &quot;\n&quot;;     
	  }
   
   /* Verify region has been entered */
   
  if (feedbackform.ggregion.value != &quot;&quot;) {
       region = 1;
       }
      
   if (region == -1) {
	  empty_fields += &quot;region&quot; + &quot;\n&quot;;     
	  }
   
   
    /* Verify gender has been entered */
   	
	for (i=0; i<feedbackform.hhgender.length; i++) {
       if (feedbackform.hhgender[i].checked) {
       gender = 1;
       }
      }
      if (gender == -1) {
	  empty_fields += &quot;gender&quot; + &quot;\n&quot;;     
	  }
	 
	  /* Verify age has been entered */	 
	 
	 for (i=0; i<feedbackform.iiage.length; i++) {
       if (feedbackform.iiage[i].checked) {
       age = 1;
       }
      }
      if (age == -1) {	
	  empty_fields += &quot;age&quot; + &quot;\n&quot;; 	 	 
      }
	 
	 
	 /* Verify faculty has been entered */	
	 
	 if (feedbackform.jjfaculty.value != &quot;&quot;) {
       faculty = 1;
       }
      
     if (faculty == -1) {
	  empty_fields += &quot;faculty&quot; + &quot;\n&quot;;     
	  }
	 

     /* Verify interest has been entered */	
	 
     if (feedbackform.kkinterest.value != &quot;&quot;) {
       interest = 1;
       }
      
     if (interest == -1) {
	  empty_fields += &quot;main area of interest&quot; + &quot;\n&quot;;     
	  }


	  /* Verify category has been entered */	
	 
	 if (feedbackform.lljobcategory.value != &quot;&quot;) {
       category = 1;
       }
      
     if (category == -1) {
	  empty_fields += &quot;job category&quot; + &quot;\n&quot;;     
	  }
	 
	 
	 
	 
	 
	 /* Verify 'issues to talk about' has been entered */
	 
	 for (i=0; i<feedbackform.mmissuestotalkabout.length; i++) {
       if (feedbackform.mmissuestotalkabout[i].checked) {
       issues = 1;
       }
      }
      if (issues == -1) {
	  empty_fields += &quot;issues to talk about&quot; + &quot;\n&quot;;     
	  }
	 
	 
 /* Check if any required fields are empty, if they are give an error message with the field name */
	 
	 if (empty_fields !=&quot;&quot;) {
     msg +=&quot;______________________________________________________\n\n&quot;
     msg += &quot;The form was not submitted because of the following error(s).\n&quot;;	 
     msg += &quot;- The following required field(s) are empty:&quot; + &quot;\n&quot;
                +empty_fields + &quot;\n&quot;;
     alert(msg);
	 
	 /* Don't submit the form */
     return false;
	 
     }
	 
	 else {	 
	 /* Submit the form */
	 return true;
	 }
	 

}
 

Steve,

Thanks for reposting using the code tags - as you can see, information was lost during the first post.

Turning the Javascript console on in Mozilla immediately showed the problem - feedbackform wasn't defined.

As it happens, IE (and other browsers) are more forgiving.

If you add the following line to the top of your verify function:

Code:
var feedbackform = document.forms['feedbackform'];

Then all will work as you expect.

Hope this helps,

Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top