By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,628 Members | 1,880 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,628 IT Pros & Developers. It's quick & easy.

check if a field is defined

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.