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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check to see if a form element exists

Status
Not open for further replies.

Slippenos

MIS
Apr 22, 2005
333
US
I have a form that may or may not contain certain form elements depending on server-side ASP conditions.

With that being said, how can I test to see if a form element is present.

I tried:
Code:
if ( document.getElementById( anElement ) != "undefined") 

   {

   my validation code

   }
AND
Code:
if ( document.getElementById( anElement ) != null )

   {

   my validation code

   }

To no avail.

Any thoughts?

[red]Tools | Internet Options | Clear History[/red]
 
Do your form fields have their own id's?
Are they unique?

You are pretty close with the above attempt:
Code:
<script type="text/javascript">
var arr = ['t1','t2'];

function ckFields(){
for(var j = 0; j < arr.length; j++){
    if(document.getElementById(arr[j]) != undefined)
    alert(document.getElementById(arr[j]).name);
  }
}
</script>
</head>
<body onload="ckFields()">
<form name="f1">
<input type="text" id="t1" name="t1" />
</form>

Thanks,
--Mark
 
Hmm...

I wanted to accomplish something similar to:


Code:
if( cssign.getElementByName( EstimateStatus ) != undefined)

	{


	if( cssign.EstimateStatus.value == "Nothing" )
	
		{
		
		alert( "You have not changed the status of the Estimate" );
		cssign.EstimateStatus.focus( );
		return( false );
		
		}

.. Whith an onsubmit event. Is this possible?

[red]Tools | Internet Options | Clear History[/red]
 
I forgot the '}' at the end.

I forgot to mention I am generating a 'EstimateStatus' is undefined error message.

[red]Tools | Internet Options | Clear History[/red]
 
Is there any reason you can't use the element name like so:

Code:
<script type="text/javascript">

function ckForm(form){
if(form.elements['EstimateStatus'] && 
   form.elements['EstimateStatus'].value == 'Nothing'){
   alert('Your Note');
   return false;
   }
if(form.elements['EstimateStatusB'] && 
   form.elements['EstimateStatusB'].value == 'Nothing'){
   alert('Your Note');
   return false;
   }
return true;      
}
</script>
</head>
<body>
<form name="f1" onSubmit="return ckForm(this)">
<input type="text" id="t1" name="t1" /><br />
<select name="EstimateStatus">
  <option selected value="Nothing" />Nothing
  <option value="Something" />Something  
</select>
<input type="submit">
</form>

What type of form fields are you working with?

Thanks,
--Mark
 
The problem was that the form fields didn't always exist. For instance ...

Code:
<script language = "javascript">
<!--

function test( )

{

if( document.form.thefield.value.length > 1 )

   {

   alert("The field 'thefield' exists!");

   }

}

//-->
</script>
Code:
<form name = "form" onsubmit = "return test( );">
Code:
<% If Len( objRS("thisfield") ) > 0 Then %>

<input type = "text" name = "thatfield">

<% Else %>

<input type = "text" name = "thefield">

<% End If %>

It was poor programming on my part, but that was the gist
of what I was doing.

However, I did resolve my issue. I said something to the tune of:

Code:
<% If Len( objRS("thisfield") ) > 0 Then %>

<input type = "text" name = "thatfield">

<% Else %>

<input type = "hidden" name = "thatfield" value = "<%=objRS("thatfield")%>">

<% End If %>

Thanks for the posts.

-Mike





[red]Tools | Internet Options | Clear History[/red]
 
Hi Slippenos,

If you notice there is no form field 'EstimateStatusB' in the above example.
That is what the first part of our 'if' does.
(Makes sure there is a form field with that name before testing the value.)

Code:
if(form.elements['EstimateStatusB'] &&
   form.elements['EstimateStatusB'].value == 'Nothing'){
   alert('Your Note');
   return false;
   }

Thanks,
--Mark
 
For a function that checks the existence of an object, see my post on thread216-1106645.

Adam
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top