Javascript form validation script- not defined in Mozilla Firefox

N

nektir

I have an external js file to validate a form and it works in IE and
Opera, but not Mozilla Firefox.

In Mozilla Firefox, the javascript console sasys "SearchForm" is not
defined in the second line

function Valid() {
if ((SearchForm.FirstName.value == "") && (SearchForm.LastName.value
== "") && (SearchForm.Phone.value == "") && (SearchForm.Dept.value ==
"%")) {
alert("Please enter criteria before submitting.");
SearchForm.FirstName.focus();
return false;
}
else if (SearchForm.Company.value == "") {
alert("Please select a company to search by.");
SearchForm.Company.focus();
return false;
}
else
SearchForm.submit();
}

Any ideas?
 
W

web.dev

I have an external js file to validate a form and it works in IE and
Opera, but not Mozilla Firefox.

In Mozilla Firefox, the javascript console sasys "SearchForm" is not
defined in the second line

function Valid() {
if ((SearchForm.FirstName.value == "") && (SearchForm.LastName.value
== "") && (SearchForm.Phone.value == "") && (SearchForm.Dept.value ==
"%")) {
alert("Please enter criteria before submitting.");
SearchForm.FirstName.focus();
return false;
}
else if (SearchForm.Company.value == "") {
alert("Please select a company to search by.");
SearchForm.Company.focus();
return false;
}
else
SearchForm.submit();
}

Any ideas?

Try using the following syntax instead, replace where necessary:

document.forms["SearchForm"].elements["FirstName"].value
document.forms["SearchForm"].elements["LastName"].value
document.forms["SearchForm"].elements["Phone"].value
etc. etc.
 
M

McKirahan

I have an external js file to validate a form and it works in IE and
Opera, but not Mozilla Firefox.

In Mozilla Firefox, the javascript console sasys "SearchForm" is not
defined in the second line

function Valid() {
if ((SearchForm.FirstName.value == "") && (SearchForm.LastName.value
== "") && (SearchForm.Phone.value == "") && (SearchForm.Dept.value ==
"%")) {
alert("Please enter criteria before submitting.");
SearchForm.FirstName.focus();
return false;
}
else if (SearchForm.Company.value == "") {
alert("Please select a company to search by.");
SearchForm.Company.focus();
return false;
}
else
SearchForm.submit();
}

Any ideas?


Are you declaring it; for example:
var SearchForm = document.forms[0];

Or is it the name of the form; as in:
<form name="SearchForm" ...>

Also, how are you calling your "Valid()" function?
 
N

nektir

I'm not declaring as it is the name of the form <form name="SearchForm"
id="SearchForm"..

I'm calling the Valid function like this:

<a href="#" onClick="Valid()"><img src="/images/go.gif" alt="Submit"
width="34" height="19"/></a>
I have an external js file to validate a form and it works in IE and
Opera, but not Mozilla Firefox.

In Mozilla Firefox, the javascript console sasys "SearchForm" is not
defined in the second line

function Valid() {
if ((SearchForm.FirstName.value == "") && (SearchForm.LastName.value
== "") && (SearchForm.Phone.value == "") && (SearchForm.Dept.value ==
"%")) {
alert("Please enter criteria before submitting.");
SearchForm.FirstName.focus();
return false;
}
else if (SearchForm.Company.value == "") {
alert("Please select a company to search by.");
SearchForm.Company.focus();
return false;
}
else
SearchForm.submit();
}

Any ideas?


Are you declaring it; for example:
var SearchForm = document.forms[0];

Or is it the name of the form; as in:
<form name="SearchForm" ...>

Also, how are you calling your "Valid()" function?
 
N

nektir

Changing the syntax fixed it along with changing the call to
Valid(document.Searchform) instead of just Valid()

Thanks for your help guys!

web.dev said:
I have an external js file to validate a form and it works in IE and
Opera, but not Mozilla Firefox.

In Mozilla Firefox, the javascript console sasys "SearchForm" is not
defined in the second line

function Valid() {
if ((SearchForm.FirstName.value == "") && (SearchForm.LastName.value
== "") && (SearchForm.Phone.value == "") && (SearchForm.Dept.value ==
"%")) {
alert("Please enter criteria before submitting.");
SearchForm.FirstName.focus();
return false;
}
else if (SearchForm.Company.value == "") {
alert("Please select a company to search by.");
SearchForm.Company.focus();
return false;
}
else
SearchForm.submit();
}

Any ideas?

Try using the following syntax instead, replace where necessary:

document.forms["SearchForm"].elements["FirstName"].value
document.forms["SearchForm"].elements["LastName"].value
document.forms["SearchForm"].elements["Phone"].value
etc. etc.
 
M

McKirahan

nektir said:
I'm not declaring as it is the name of the form <form name="SearchForm"
id="SearchForm"..

I'm calling the Valid function like this:

<a href="#" onClick="Valid()"><img src="/images/go.gif" alt="Submit"
width="34" height="19"/></a>

AFAIK, the proper way to reference it is by
"document.SearchForm" not simply "SearchForm".

JavaScript Form Contents
http://www.irt.org/script/form.htm


Q299 When referring to my form using form.field.value
it does not work, why?
http://www.irt.org/script/299.htm

[snip]
 
M

McKirahan

nektir said:
Changing the syntax fixed it along with changing the call to
Valid(document.Searchform) instead of just Valid()
[snip]

Did you also change the above to the following?

function Valid(SearchForm) {
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Wed, 4 Oct 2006 16:50:25 remote, seen in
news:comp.lang.javascript said:
Try using the following syntax instead, replace where necessary:

document.forms["SearchForm"].elements["FirstName"].value
document.forms["SearchForm"].elements["LastName"].value
document.forms["SearchForm"].elements["Phone"].value
etc. etc.


var SFe = document.forms["SearchForm"].elements
SFe["FirstName"].value
SFe["LastName"].value
SFe["Phone"].value

should be both faster and smaller.
 
R

RobG

I have an external js file to validate a form and it works in IE and
Opera, but not Mozilla Firefox.

In Mozilla Firefox, the javascript console sasys "SearchForm" is not
defined in the second line

function Valid() {
if ((SearchForm.FirstName.value == "") && (SearchForm.LastName.value [...]

Any ideas?

It pays to read the FAQ:

<URL: http://www.jibbering.com/faq/#FAQ4_41 >
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top