Click to See Complete Forum and Search --> : Why doesn't this script work in Netscape 4?


damainman77
07-11-2003, 09:02 AM
I am designing a user application form that uses Javascript for form validation. A lot of the users will have older browsers, though nearly all will support Javascript. I tested the form on Netscape 4.79 or so, and the pop-up windowsfor empty fields simply do not some up on submit. Is this because of the"form.focus" part? Here are all my If statements and all. They work just fine on IE and Mozilla.

function validateForm(form) {

if (form.FirstName.value == "") {
alert("Please enter your first name.");
form.FirstName.focus( );
return false;
}
if (form.LastName.value == "") {
alert("Please enter your last name.");
form.LastName.focus( );
return false;
}
if (form.PhoneNumber.value == "") {
alert("Please enter your work phone number.");
form.PhoneNumber.focus( );
return false;
}
if (form.Location.value == "") {
alert("Please enter your location.");
form.Location.focus( );
return false;
}
if (form.UserName.value == "") {
alert("Please enter your Desired User Name.");
form.UserName.focus( );
return false;
}
if (form.AlternateUserName.value == "") {
alert("Please enter an Alternate User Name.");
form.AlternateUserName.focus( );
return false;
}
}
</script>


------------------------------------------------------

In the html, I call this function like so......

<form onsubmit="return validateForm(this)">

Here is my submit button, but I seriously doubt that is the problem....

<input type="submit" name="submit" value="Submit">


Any help would be greatly appreciated.

damainman77
07-11-2003, 10:32 AM
Would it help if I posted the ENTIRE script, along with the whole integrated HTML form? I'm kind of desperate, i anyone has a clue about this at all, please voice your opinion.

olerag
07-11-2003, 11:14 AM
The following code works on both IE/Netscape7 and, I believe, on Netscape4.7x.

Note that I'm using "text" fields for validation, am not passing the "form" object to the function call, and am specifically using an array element assignment when distiguishing objects in the form (i.e., "forms[0]").

<html>
<head>
<SCRIPT Language = "JavaScript">

function validateForm() {
if (performValidation() == true) {
alert("Validation was successful.");
}
}

function performValidation() {
var result = true;

if (document.forms[0].field1.value == "") {
document.forms[0].field1.focus();
alert("The first field must contain and entry.");
result = false;
}

if (result) {
if (document.forms[0].field2.value == "") {
document.forms[0].field2.focus();
alert("The second field must contain and entry.");
result = false;
}
}

return(result);
}
</SCRIPT>
</head>
<body>
<form>
<center>
<b>Sample Fields</b>
</center>

<p>
<input type="text" name="field1" size="25">
<br>
<input type="text" name="field2" size="25">

<center>
<p>
<input type="button" value="Validate" onClick="validateForm()">
</center>
</form>
Pressing submit will validate the fields.
</body>
</html>

damainman77
07-11-2003, 12:47 PM
By the way, JavaScript is enabled on the browser, in case that wasn't clear (I don't think it was). I didn't think JavaScript had changed much since Netscape 4.7, but apparently it has.

damainman77
07-11-2003, 12:49 PM
Thanks, olerag, I'll give that a try. My script seems to work with everything higher than Netscape 4.7x, but that isn't good enough considering the user base I will have. Thanks again.

gil davis
07-11-2003, 01:02 PM
When having problems with NS 4.x, here are some things you can check:

1. Proper capitalization for events.
2. The JavaScript Console (type "javascript:" in the location bar.
3. View | Page Source and look for broken HTML tags. Valid tags ar bold blue, valid attributes are bold black, literals are blue, comments are black italic, broken things blink.

Also, your validate function should return true as the default fall-through condition. I don't think you are returning anything if all the tests pass.