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

Finding a variable's type

Status
Not open for further replies.

GMoyle

Programmer
Jul 13, 2001
7
GB
I've written some validation routines that take a form field as a parameter.

I want to adapt the routines so that they can take either form fields OR strings. I therefore need to be able to test for the parameter's type because if it's a field I'll use ParamName.value to get the value, where as if it's a String this will throw an error.

I've studied Java and think there's a method called .instanceOf which can be applied to objects, but what type is a Form Field and how is this method used?

Thanks!
Gareth.
 
hi Gareth,

use "typeof":

function foo(sArgument) {
if (typeof sArgument == "string")
// do stuff...
else if (typeof sArgument == "object")
// do other stuff...
} ======================================

if (!succeed) try();
-jeff
 
Thanks Jeff - That seems to be working OK. Don't suppose you know the 'type' for a Form field do you?

Thanks

Gareth.
 
i believe it should display as "object"

test it:

<input type=&quot;button&quot; value=&quot;show typeof&quot; onclick=&quot;alert(typeof this);&quot; />
======================================

if (!succeed) try();
-jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top