469,306 Members | 1,797 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

check if a field is defined

Hi,
how can I check if an input field in a form is defined? I create the field
dynamically, so it could or could not be in the form.

TIA.
Jul 23 '05 #1
6 12108
Xerxes wrote:
Hi,
how can I check if an input field in a form is defined? I create the
field dynamically, so it could or could not be in the form.


if (document.forms['formName'].elements['elementName']) {
// Element name is defined
}

Instead of formName and elementName you can also use indexes.
JW

Jul 23 '05 #2
I am hving the problem inside a function that is validating form fields.
It takes the form name as argument. I used your example, with some
modifications but it does not seem to work:

function validate(theForm){
....
if (theForm.FR) { // FR is the field name, which is a group of radio
buttons
if (!getRadioButtonChecked(theForm.FR)) {
alert("Please make a selection ....")
return false;
}
}
....
}

Then in the form:
<form name..... onSubmit="return validate(this)>
....
<?php
if (some condition)
<input name="FR" type="radio" value=""
....
?>
So, the function getRadioButtonChecked is not supposed to be called if FR is
not defined, but it does get called and it chokes if the field is not
defined.

Isn't there something like "isdefined" or "isset" function in javascript?

Thanks.
"Janwillem Borleffs" <jw@jwscripts.com> wrote in message
news:40***********************@news.wanadoo.nl...
Xerxes wrote:
Hi,
how can I check if an input field in a form is defined? I create the
field dynamically, so it could or could not be in the form.


if (document.forms['formName'].elements['elementName']) {
// Element name is defined
}

Instead of formName and elementName you can also use indexes.
JW

Jul 23 '05 #3
Xerxes wrote:
function validate(theForm){
...
if (theForm.FR) { // FR is the field name, which is a group of radio
[...] Isn't there something like "isdefined" or "isset" function in
javascript?


Sure, you can use typeof to see if the object doesn't evaluate to
'undefined', but the code in my previous reply has the same result with a
shorter notation.

Your problem, however, is that you are not accessing the element through the
elements collection, try:

if (theForm.elements['FR']) {
...
}
JW

Jul 23 '05 #4
Hi,
You can try "undefined"

....
if ((theForm.FR.value=="undefined")) {
alert("Please make a selection ....")
return false;
}

Kien

"Xerxes" <as******@hotmail.com> wrote in message news:<39******************************@news.terane ws.com>...
I am hving the problem inside a function that is validating form fields.
It takes the form name as argument. I used your example, with some
modifications but it does not seem to work:

function validate(theForm){
...
if (theForm.FR) { // FR is the field name, which is a group of radio
buttons
if (!getRadioButtonChecked(theForm.FR)) {
alert("Please make a selection ....")
return false;
}
}
...
}

Then in the form:
<form name..... onSubmit="return validate(this)>
...
<?php
if (some condition)
<input name="FR" type="radio" value=""
...
?>
So, the function getRadioButtonChecked is not supposed to be called if FR is
not defined, but it does get called and it chokes if the field is not
defined.

Isn't there something like "isdefined" or "isset" function in javascript?

Thanks.

Jul 23 '05 #5
Kien wrote:
Hi,
You can try "undefined"

....
if ((theForm.FR.value=="undefined")) {
alert("Please make a selection ....")
return false;
}


That won't tell you if the field is defined or not, it will tell you if
it's value is "undefined", a string.

<input type="text" value="undefined" name="FR"> will pass that test,
even though its "defined".

Which is not what the OP wanted.

Please read the FAQ with regards to top-posting.
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/
Jul 23 '05 #6
Thanks a lot, it is working...
For some reason, the code being executed on the server (when I did "view
source") was different than what I was seeing through the FTP terminal
(WS-FTP) or through logging in on the admin page of the web server. It seems
the old page, that did not have the correction in it was being executed,
even though the new page was being showed up through View Source. I have no
idea what was going on. But it is OK now.

Thanks again for your help.

"Janwillem Borleffs" <jw@jwscripts.com> wrote in message
news:40***********************@news.wanadoo.nl...
Xerxes wrote:
function validate(theForm){
...
if (theForm.FR) { // FR is the field name, which is a group of radio
[...]
Isn't there something like "isdefined" or "isset" function in
javascript?


Sure, you can use typeof to see if the object doesn't evaluate to
'undefined', but the code in my previous reply has the same result with a
shorter notation.

Your problem, however, is that you are not accessing the element through

the elements collection, try:

if (theForm.elements['FR']) {
...
}
JW

Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Lorenzo Bolognini | last post: by
5 posts views Thread by Steve Wylie | last post: by
19 posts views Thread by wetherbean | last post: by
1 post views Thread by WebBuilder451 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.