Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Checking to see if a field exists in Javascript

Posted on 2000-03-15
8
Medium Priority
?
268 Views
Last Modified: 2013-12-18
In Javascript, is there a way to check if a field exists before referring to it?
 
The reason I ask is whenever my document loads I execute the following passthrough Javascript.

<Script Language="Javascript">
{if (document.forms[0].customerName.value =="Error") {alert("The customer number is invalid");document.forms[0].customerNumber.focus()}}
</Script>

This works great as long as the document is in edit mode, however, if I try to open the document in read mode I get the error "document.forms[0].customerNumber is not an object".  Because the document is in read mode it doesn't see the customerNumber field.  How can I skip this code if the document is opened in read mode?  I can't use hide-when's because if I do the HTML style is taken away from my passthrough HTML.
0
Comment
Question by:snobunny
  • 3
  • 3
  • 2
8 Comments
 
LVL 6

Expert Comment

by:ghassan99
ID: 2623056
What version of Notes r u using?
You can use hide/when without losing the passthrough HTML property, and your script will generate no errors.

-Gus
0
 
LVL 24

Expert Comment

by:HemanthaKumar
ID: 2623868
Hi

You can test whether an object is available or not by testing for undefined value, here is the sample of it:
[<Body onLoad="test()">]

<SCRIPT Language="Javascript">
function test(){
      if (document.forms[0].customerName==undefined)
      alert("True");
}
</SCRIPT>




Good Luck
~Hemanth
0
 

Author Comment

by:snobunny
ID: 2625183
Your example works in Netscape 4.08 (Actually my code always worked in Netscape) but still does not work in IE 4.0

I now get the following error message:  

Error: 'undefined' is undefined
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 6

Accepted Solution

by:
ghassan99 earned 800 total points
ID: 2625881
I dont think its actually working in Netscape!  But probably netscape is more tolerant.  The proper way to test for a field is using the typeof operator, without that u r actually checking if the field has value or not.
Use it like this:

<Script Language="Javascript">
if (typeof document.forms[0].customerName != "undefined") {    
{if (document.forms[0].customerName.value =="Error") {alert("The customer number is invalid");document.forms[0].customerNumber.focus()}}}
</Script>

And I still think that hiding the text will not remove its passthru property.

-Gus
0
 
LVL 24

Expert Comment

by:HemanthaKumar
ID: 2626569
Hi All

I have tested my code on NS and found to work, but I curse myself for not testing on the IE, anyway as gus mentioned typeof is preferable and works perfectly on both browsers.

~Hemanth
0
 

Author Comment

by:snobunny
ID: 2628751
Your solution worked in NS but did not work on IE.  The solution that Gus provided worked on both.  I will award the points to Gus.  Thanks for answering my question.
0
 

Author Comment

by:snobunny
ID: 2628756
Thanks for your answer, Gus.  I have tried it out and it works great!
0
 
LVL 6

Expert Comment

by:ghassan99
ID: 2630172
No problem...:)

-Gus
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For Desktop Techs: How to retain a user's Notes configuration data when swapping out the end user's computer. (Assuming that you are not upgrading to a completely different version of Notes client) All you need to do is: 1) install Notes o…
For users on the Lotus Notes 8 Standard client, this article provides information on checking the Java Heap size and adjusting it to half of your system RAM in attempt to get the Lotus Notes 8.x Standard client to run faster.  I've had to exercise t…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question