473,583 Members | 2,858 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Form name not working

CJ
Why won't this work?
I am passing the name of the form (I have two that use this validation
script) but I keep getting an error.
Error reads: "document.which _form.name is null or not an object"

HTML-----------
Form is ----> <form action="thanks. php" method="post" name="contact_f orm"
id="contact_for m">
Name -------> <input type="text" name="name" id="name" size="25">
Button sends code -----> <input type="button" value="Submit Form"
onClick="contac t_validate('con tact_form')">

JAVASCRIPT-----------
function contact_validat e(which_form) {
var field_errors = "The following field(s) are required: \n\n";
var errors = 0;
// If name is null
if (document.which _form["name"].value == '') {
//alert("Please enter your name");
//return;
field_errors = field_errors+"N ame - Please enter your name \n";
errors++;
}

Thanks,
CWJ
Jul 20 '05 #1
12 2453
CJ wrote:
Why won't this work?
I am passing the name of the form (I have two that use this validation
script) but I keep getting an error.
Error reads: "document.which _form.name is null or not an object"

HTML-----------
Form is ----> <form action="thanks. php" method="post"
name="contact_f orm" id="contact_for m">
Name -------> <input type="text" name="name" id="name" size="25">
Button sends code -----> <input type="button" value="Submit Form"
onClick="contac t_validate('con tact_form')">

JAVASCRIPT-----------
function contact_validat e(which_form) {
var field_errors = "The following field(s) are required: \n\n";
var errors = 0;
// If name is null
if (document.which _form["name"].value == '') {
//alert("Please enter your name");
//return;
field_errors = field_errors+"N ame - Please enter your name \n";
errors++;
}

Thanks,
CWJ

1) Move the form validation to the <form>'s onsubmit handler - you don't need to
pass a reference to the form:

<form action="thanks. php" method="post" onsubmit="retur n contact_validat e()">
<input type="text" name="name" id="name" size="25"/>
<input type="submit" value="Submit Form">
</form>

2) Update your validation function

- reference the 'name' field using document.getEle mentById()
- return false if there are errors - so the form *doesn't* submit
- return true if there are no errors - so the form *does* submit

function contact_validat e() {
var field_errors = "The following field(s) are required: \n\n";
var errors = 0;
// If name is null
if ( document.getEle mentById( 'name' ).value == '') {
field_errors = field_errors+"N ame - Please enter your name \n";
errors++;
}
if ( errors > 0 ) {
alert( field_errors);
return false;
}
return true
}
Jul 20 '05 #2
cwj
Thanks, that did the trick.

I appreciate the help.

CWJ
"DB McGee" <no*****@norepl y.com> wrote in message
news:fL******** ***********@new s01.bloor.is.ne t.cable.rogers. com...
CJ wrote:
Why won't this work?
I am passing the name of the form (I have two that use this validation
script) but I keep getting an error.
Error reads: "document.which _form.name is null or not an object"

HTML-----------
Form is ----> <form action="thanks. php" method="post"
name="contact_f orm" id="contact_for m">
Name -------> <input type="text" name="name" id="name" size="25">
Button sends code -----> <input type="button" value="Submit Form"
onClick="contac t_validate('con tact_form')">

JAVASCRIPT-----------
function contact_validat e(which_form) {
var field_errors = "The following field(s) are required: \n\n";
var errors = 0;
// If name is null
if (document.which _form["name"].value == '') {
//alert("Please enter your name");
//return;
field_errors = field_errors+"N ame - Please enter your name \n";
errors++;
}

Thanks,
CWJ

1) Move the form validation to the <form>'s onsubmit handler - you don't
need to
pass a reference to the form:

<form action="thanks. php" method="post" onsubmit="retur n
contact_validat e()">
<input type="text" name="name" id="name" size="25"/>
<input type="submit" value="Submit Form">
</form>

2) Update your validation function

- reference the 'name' field using document.getEle mentById()
- return false if there are errors - so the form *doesn't* submit
- return true if there are no errors - so the form *does* submit

function contact_validat e() {
var field_errors = "The following field(s) are required: \n\n";
var errors = 0;
// If name is null
if ( document.getEle mentById( 'name' ).value == '') {
field_errors = field_errors+"N ame - Please enter your name \n";
errors++;
}
if ( errors > 0 ) {
alert( field_errors);
return false;
}
return true
}

Jul 20 '05 #3
"DB McGee" <no*****@norepl y.com> wrote in message
news:fL******** ***********@new s01.bloor.is.ne t.cable.rogers. com...
<snip>
1) Move the form validation to the <form>'s onsubmit handler -
you don't need to pass a reference to the form:

<form action="thanks. php" method="post"
onsubmit="retur n contact_validat e()">
On the contrary, calling the validation function from the onsubmit
handler is an ideal opportunity to anonymously pass a generalised
validation function a reference to the FORM element that it is to be
applied to. In the internally generated event handling function that is
created using the onsubmit attribute string the - this - keyword is
always a reference to the FORM element to which the function is assigned
as a method:-

onsubmit="retur n contact_validat e(this);"

- Removes the need to be interested in the name of the form.
<input type="text" name="name" id="name" size="25"/> <snip>

"name" could be a bad name for a form element and also for the ID of an
element as the W3C HTML DOM specification requires that named and/or
IDed properties are made available as named properties of the FORM
element, so the above INPUT element would be available as a property of
the form under the name "name". However, the FORM element is also
required to have a property with the name "name" that represents the
name of the form.

Implementations may choose to replace the name property of the form with
a reference to the INPUT element with the name "name" or they might not,
the grounds for that decision are not specified. Generally it is unwise
to give form elements names (or IDs) that correspond with existing
properties of a FORM element, especially if those properties are going
to be used. As property names are case sensitive it is only necessary,
for exempt, to capitalise the name/ID to avoid the risk of conflicts.
2) Update your validation function

- reference the 'name' field using document.getEle mentById()

<snip>

If the point of the function is to provide a general function for use
with multiple forms, and the ID of the element is to be hard coded in
the function then the strategy will not work as the getElementById
method is required to return the first element on the page with the
corresponding ID, so it will always return a reference to the INPUT
element IDed as "name" in the first form on the page. But HTML requires
that an ID be unique on the page so hard coding an ID string into a
function is not viable if the function is intended to act with more than
one element.

The result is that if the string used to look up the reference to an
INPUT element is to be hard coded into a general validation function
then an ID cannot be used, and so getElementById is an inappropriate
method of acquiring the required reference.

Given a need to resolve a reference to a named INPUT element within a
form, where the same method and name string must be used with several
forms, the best place to look-up the reference to the input element is
as a named property of the - elements - collection of the form. An
approach that conforms with the W3C HTML DOM specification and is also
back-compatible with every JavaScript capable browser that also
understands what a form is:-

function contact_validat e(formRef) {
var field_errors = "The following field(s) are required: \n\n";
var errors = 0;
if ( formRef.element s[ 'name' ].value == '') {
field_errors = field_errors+"N ame - Please enter your name \n";
errors++;
}
if ( errors > 0 ) {
alert( field_errors);
return false;
}
return true
}

Richard.
Jul 20 '05 #4
In article <fL************ *******@news01. bloor.is.net.ca ble.rogers.com> , "DB
McGee" <no*****@norepl y.com> writes:
CJ wrote:
Why won't this work?
I am passing the name of the form (I have two that use this validation
script) but I keep getting an error.
Error reads: "document.which _form.name is null or not an object"
thats because which_form is a variable, see below for a solution.
HTML-----------
Form is ----> <form action="thanks. php" method="post"
name="contact_f orm" id="contact_for m">
Name -------> <input type="text" name="name" id="name" size="25">
Button sends code -----> <input type="button" value="Submit Form"
onClick="contac t_validate('con tact_form')">

JAVASCRIPT-----------
function contact_validat e(which_form) {
var field_errors = "The following field(s) are required: \n\n";
var errors = 0;
// If name is null
if (document.which _form["name"].value == '') {
document.forms[which_form].elements['name'].value
//alert("Please enter your name");
//return;
field_errors = field_errors+"N ame - Please enter your name \n";
errors++;
}

Thanks,
CWJ

1) Move the form validation to the <form>'s onsubmit handler - you don't need
to pass a reference to the form:

<form action="thanks. php" method="post" onsubmit="retur n contact_validat e()">
<input type="text" name="name" id="name" size="25"/>
<input type="submit" value="Submit Form">
</form>

2) Update your validation function

- reference the 'name' field using document.getEle mentById()


And break it in browsers that don't support it? When the document.forms
collection can handle it in any browser that supports forms?
- return false if there are errors - so the form *doesn't* submit
- return true if there are no errors - so the form *does* submit

function contact_validat e() {
var field_errors = "The following field(s) are required: \n\n";
var errors = 0;
// If name is null
if ( document.getEle mentById( 'name' ).value == '') {
The OP stated that he had two forms that use the same function, so it should be
reasonably safe to assume that each form has a field with name="name" (not sure
I would name it name, perhaps fName or such), then the browser will never get
one of them. Utterly breaks the script.
field_errors = field_errors+"N ame - Please enter your name \n";
errors++;
}
if ( errors > 0 ) {
alert( field_errors);
return false;
}
return true
}

--
Randy
Jul 20 '05 #5
"Richard Cornford" <Ri*****@litote s.demon.co.uk> writes:

....
as the W3C HTML DOM specification requires that named and/or
IDed properties are made available as named properties of the FORM
element,


Do you have a reference for that? I don't see anything about that in
the ECMAScript bindings for W3C HTML DOM (1 or 2, they agree) under
HTMLFormElement . It has the "elements" collection, but that is it.

All existing browsers do it this way, but it's not in the W3C DOM as
far as I can see (and shouldn't be, as it's polluting the namespace
and giving exactly the problems you point out).

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #6
"Lasse Reichstein Nielsen" <lr*@hotpop.com > wrote in message
news:4q******** **@hotpop.com.. .
"Richard Cornford" <Ri*****@litote s.demon.co.uk> writes:

...
as the W3C HTML DOM specification requires that named and/or
IDed properties are made available as named properties of the
FORM element,
Do you have a reference for that? I don't see anything about
that in the ECMAScript bindings for W3C HTML DOM (1 or 2, they
agree) under HTMLFormElement . It has the "elements" collection,
but that is it.


<quote>
Interface HTMLFormElement
The FORM element encompasses behavior similar to a collection
and an element. It provides direct access to the contained form
controls as well as the attributes of the form element.
...
length of type long, readonly
The number of form controls in the form.
</quote>

That is my interpretation of "behavior similar to a collection" and
"direct access to the contained form controls". On the grounds that if
it is a collection it has to be a collection of something and the form
elements ore the obvious candidate, and I would describe making the
elements available through the - elements - collection as indirect.
All existing browsers do it this way, but it's not in the W3C
DOM as far as I can see (and shouldn't be, as it's polluting
the namespace and giving exactly the problems you point out).


A lot of the HTML DOM is a formalisation of existing browser behaviour,
but it probably was a mistake for the W3C to do this (assuming you
cannot find a reasonable alternative interpretation0 .

Richard.
Jul 20 '05 #7
"Richard Cornford" <Ri*****@litote s.demon.co.uk> writes:
<quote>
Interface HTMLFormElement
The FORM element encompasses behavior similar to a collection
and an element. It provides direct access to the contained form
controls as well as the attributes of the form element.
...
length of type long, readonly
The number of form controls in the form.
</quote>
I can see why this can be taken to mean that the HTML element is a
collection itself. However, the IDL in DOM 2 HTML can be used in
strictly typed OO languages, like Java or C++, to create objects
implementing the HTMLFormElement interface. They have no provision
for adding arbitrarily named propertie to such objects.

With that in mind, I read the "compasses behavior similar to a
collection" as merely saying what should be possible through the
FormElement, not how it is implemented. The implementation makes
the collection behavior available through the "elements" collection.

Compare it to the HTMLSelectEleme nt. It says:
---
The contained options can be directly accessed through the select
element as a collection.
---
Again, I could read it as saying that a select element should act
as a collection, but all the interface requires is that it *contains*
the options collection. Select elements also have a length property
which duplicates the one in the options collection.

So, reading the DOM 2 IDL as a specification for Java, there is no way
forms or selects can be collections - they don't have the appropriate
methods (item, namedItem). There is nothing in the ECMAScript binding
that makes it special (as there is for collections, allowing access
through the [...] property accessor).
That is my interpretation of "behavior similar to a collection" and
"direct access to the contained form controls". On the grounds that if
it is a collection it has to be a collection of something and the form
elements ore the obvious candidate, and I would describe making the
elements available through the - elements - collection as indirect.
The elements property is read-only and is bound to this particular
form. Thinking in OO modelling terminology, that makes it a component
of the form element, not just reference by it. Having a collection
component would count as direct enough for me.
A lot of the HTML DOM is a formalisation of existing browser behaviour,
but it probably was a mistake for the W3C to do this (assuming you
cannot find a reasonable alternative interpretation0 .


I try :) But only because your interpretation makes no sense in, e.g.,
Java with the defined interface, and there is no sign that the
ECMAScript binding is doing anything special.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #8
On Sat, 10 Jan 2004 12:05:24 +0100, Lasse Reichstein Nielsen
<lr*@hotpop.com > wrote:
"Richard Cornford" <Ri*****@litote s.demon.co.uk> writes:
A lot of the HTML DOM is a formalisation of existing browser behaviour,
but it probably was a mistake for the W3C to do this (assuming you
cannot find a reasonable alternative interpretation0 .


I try :) But only because your interpretation makes no sense in, e.g.,
Java with the defined interface, and there is no sign that the
ECMAScript binding is doing anything special.


I'm inclined with Richard here, but the DOM WG are very good at
responding to questions and clarify their testcases and more if you
ask such questions on the list (they recently clarified what
setAttributeNS( 'urn://foo','chicken', 'test') does for me) I'd
encourage everyone to ask them if they're unsure, it's just an email
to ww*****@w3.org after all.

Jim.
--
comp.lang.javas cript FAQ - http://jibbering.com/faq/

Jul 20 '05 #9
"Lasse Reichstein Nielsen" <lr*@hotpop.com > wrote in message
news:ek******** **@hotpop.com.. .
<snip>
Compare it to the HTMLSelectEleme nt. It says:
---
The contained options can be directly accessed through the select
element as a collection.
---
Again, I could read it as saying that a select element should act
as a collection, but all the interface requires is that it *contains*
the options collection. Select elements also have a length property
which duplicates the one in the options collection.
It could be read that way, and I think that it might have been, as
SELECT elements on some modern browsers do make the options available as
indexed (and possibly named) properties. So:-

sel.options[sel.selectedInd ed]

-refers to the same option object as:-

sel[sel.selectedInd es]

And just to muddy the waters a bit more they also implement - item -
and - namedItem -.
So, reading the DOM 2 IDL as a specification for Java, there is
no way forms or selects can be collections - they don't have the
appropriate methods (item, namedItem). ...

<snip>

The Java argument is good, though it is clear from the HTMLElement
interface that all of the other elements are expected to also implement
that interface. So a Java object could implement HTMLCollection in
addition to HTMLElement and HTMLFormElement . It doesn't work like that
for the browser form objects as they don't implement - item - or -
namedItem - in practice, and if an HTMLFormElement implementing object
is intended to implement HTMLCollection there seems no point in
including - length - in HTMLFormElement .

Oh well, it probably will be easier to do as Jim suggests and ask them
what they intended (might encourage them to be a bit less ambiguous in
the next version) but I have the FAQ to worry about at the moment so not
for a week or two.

On the whole I still don't see having an elements collection as
sufficient to justify the "behavior similar to a collection" statement.

Richard.
Jul 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
2951
by: dan | last post by:
I have a script that selects a value in an option in a selection list according to how many letters were entered in an input box. I have it working but I am limited to what I can name the form fields because they are dynamically created by a PHP script. I can get it to work with simple name but not with names outputted by the script. Is...
25
10192
by: Lyn | last post by:
Hi, I am working on a genealogy form. The only table (so far) lists everybody in the family, one record per person. Each record has an autonum ID. The parent form (frmMainForm) displays the data in each record, which includes the ID of the father and the mother (who also have records in the table). One record per form. I have a Tab...
6
6315
by: Leszek | last post by:
Hi. I wrote a script: function zmiana(ile){ while(document.getElementById('accomp').childNodes.length>1){ ostatni=document.getElementById('document.dane.accomp').lastChild; document.getElementById('document.dane.accomp').removeChild(ostatni);
5
2948
by: WilliamRLinden | last post by:
Hi world! we are pretty new to JavaScript and have been struggling for now 2 days on this problem ... We would appreciate mercy if anyone can give us some. Basically we are trying to simulate the tab key when the down arrow key is pressed. (we know there are other way to control focus flow but we use a lot of dynamic jsp fields, that will...
6
6887
by: Harshpandya | last post by:
Hi all, I am working on the form in which you fill out the whole PHP form and e mail that details to someone. It is working fine. But now i want to send the same form to be sent to different people and user should be able choose the check boxes to whom they want to send e mail to. I write some code simple If else conditions but i think i am...
2
4439
by: punitshrivastava | last post by:
Hi to All. I am Punit Shrivastava.I am working in Asp.As i am new in this technology please help me. I want to create enquiry form in Asp. For this i code like this: <form name="enquiry" method="post" action="enquiry.php"> <table width="494" border="0" cellspacing="0" cellpadding="2" height="511"> <tr> <td colspan="2"...
12
3815
by: colt28 | last post by:
Ok so I found an ajax contact form script but i can't get the b****** to work. I made a bunch of alterations to it and it didn't work so i replaced everything with the original and it still didn't work...dunno why, but anyhow. The form appears after a delay on the page in a hidden DIV. The original form just had name, email and message, but i...
11
2261
by: Twayne | last post by:
Hi, Newbie to PHP here, no C or other relevant background, so pretty niave w/r to the nuances etc. but I think this is pretty basic. XP Pro, SP2+, PHP 4.4.7, XAMPP Local Apache Server 6.something I think and running as a service, Using NoteTab Pro as an IDE (works well). If you need more, just ask. In one functioning form:
1
3093
by: printline | last post by:
Hello All I'm quite new to xml vs. PHP, so i hope someone can help with an issue i have been struggeling with. I have an html form, that when submitted, it should create an xml file, and save in a certain place on my server. I know that this can be done using php, but i'm not sure how....? Here is my php (which needs to be configured...
3
2128
by: ClanKeithROoF | last post by:
I'm working on a calendar that works well in all points except one. I found the code online and modified it to fit my needs. One of my mods isn't working. I'm trying to make it display the chosen month under the drop down box but I'm getting and "Element MONTH_VAL is undefined in FORM" error. It seems to be defined to me but I could be missing...
0
7811
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8159
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8314
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8185
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5366
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3811
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3836
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1416
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1147
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.