Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I check to see if a field exists on a form? 3

Status
Not open for further replies.

RottPaws

Programmer
Mar 1, 2002
478
US
I've got an HTML form that is dynamically generated from a per/cgi script. Depending on various conditions certain fields may or may not be included on the form.

onSubmit runs a form validation function prior which makes sure required fields are populated. Certain fields are required if they are on the form, and not if they aren't (obviously).

Is there some sort of "if exists"-type function in JavaScript I can use to only try to validate the field if it's there?

_________
Rott Paws

...It's not a bug. It's an undocumented feature!!!
 
If you're wondering if the field "firstName" exists, for example, on the only form on the page:

Code:
if([b]document.forms[0].firstName[/b])
{
 ...
}

That will only proceed if there IS a field called firstName.

'hope that helps.

--Dave
 
Thank you very much!

[peace]

_________
Rott Paws

...It's not a bug. It's an undocumented feature!!!
 
Make sure you don't use that technique for variables though. Particularly for boolean variables that might be false, strings that might be empty, or numbers that could be zero as they will evaluate to "false". For consistency's sake, you could use the following function:
Code:
function isExisting(obj){
  return typeof(obj)!='undefined';
}

Adam
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top