JavaScript Validation Works In Firefox But Not IE

S

SJ Carter

Hello guys,

I have the following piece of code:
function validate()
{
var f = document.form1;
if (f.name.value == "" || f.name.value == '' || f.name.value.length
== 0)
{
alert('Please enter your name');
return false;
}
return true;
}


It works fine in Firefox, and when I use firebug to inspect the code I
don't get any errors. However, it doesn't work for IE. Any reasons
for that, and how can I fix this problem? Thanks.
 
J

Joost Diepenmaat

SJ Carter said:
Hello guys,

I have the following piece of code:
function validate()
{
var f = document.form1;
if (f.name.value == "" || f.name.value == '' || f.name.value.length
== 0)
{
alert('Please enter your name');
return false;
}
return true;
}


It works fine in Firefox, and when I use firebug to inspect the code I
don't get any errors. However, it doesn't work for IE. Any reasons
for that, and how can I fix this problem? Thanks.

You probably really shouldn't have a form element named "name".
 
J

Joost Diepenmaat

Joost Diepenmaat said:
You probably really shouldn't have a form element named "name".

or if you do, you can probably use form.elements.name.value instead of
form.name.value
 
S

SJ Carter

or if you do, you can probably use form.elements.name.value instead of
form.name.value

Thanks Joost, but it still isn't working. I tried changing the form
element name with no success, and then tried using the
"form.elements....", with identical results.
 
J

Joost Diepenmaat

SJ Carter said:
Thanks Joost, but it still isn't working. I tried changing the form
element name with no success, and then tried using the
"form.elements....", with identical results.

it'd probably help if you post a link to the complete page.
 
T

Thomas 'PointedEars' Lahn

SJ said:
Thanks Joost, but it still isn't working.

Post the error message, then.
I tried changing the form element name with no success, and then tried
using the "form.elements....", with identical results.

DOM terms collide with markup terms here. Just to clarify, you should not
have a form *control* named "name", i.e. you should not have

<input name="name" ...>

because the form *element*

<form ... name="foo">
...
</form>

would then cause formRef.name to be evaluated to "foo" instead of the input
object. Modifying or removing the `name' attribute value of the `form'
element would not change that, which could explain what you observed.

You have to change the value of the `name' attribute of the `input' element
instead. BTW, "submit" is another name that should be avoided, as form
objects have a submit() method.

Please trim your quotes.


PointedEars
 
S

SJ Carter

Post the error message, then.


DOM terms collide with markup terms here. Just to clarify, you should not
have a form *control* named "name", i.e. you should not have

<input name="name" ...>

because the form *element*

<form ... name="foo">
...
</form>

would then cause formRef.name to be evaluated to "foo" instead of the input
object. Modifying or removing the `name' attribute value of the `form'
element would not change that, which could explain what you observed.

You have to change the value of the `name' attribute of the `input' element
instead. BTW, "submit" is another name that should be avoided, as form
objects have a submit() method.

Please trim your quotes.

PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann

Got it working. Turns out it wasn't even that function itself, but
some embedded object that was messing things up. Thanks for all your
help!
 
S

SAM

SJ Carter a écrit :
Thanks Joost, but it still isn't working. I tried changing the form
element name with no success, and then tried using the
"form.elements....", with identical results.


Give another name than 'name' to your element would fix the trouble.

but the right use to get an element named 'name' and from the form named
'form1' is :

document.forms['form1'].elements['name']
or :
document.form1['name']

It is aways a good idea to do not use generic terms to name something
(here 'name' is an attribute and IE can misunderstand of what name you talk)
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top