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

Find Value in List

Status
Not open for further replies.

ddfff

MIS
Sep 27, 2002
125
0
0
US
I have an alert function I am trying to use similair to below.

if(document.selectcomm.reportid.value == "")
{
alert("My Alert");
return false;
}

However I want to modify it so that it checks for a specific value in a list rather than if "">

For example:

I want to run the Alert if the value of document.selectcomm.reportid is one of the following items: reportA,reportB,reportC

The list of items will be dynamic and could be different every time the page is loaded, it comes for the DB.


Thanks very much in advance.
 
Try this...
Code:
var allowedValues = [ "blah", "foo", "bar" ];
if (inArray( document.selectcomm.reportid.value, allowedValues ))
{	...
}

function inArray( vValue, aArray )
{	for( bFound = false, i=0; !bFound && i<aArray.length; i++ )	bFound = (aArray[i]== vValue);
	return bFound;
}
 
Thanks but I am having trouble getting the alert to display. Right now I get nothing.

I pasted the code for in Array function exactly and have the following:

function CheckDupe()
{
if
(inArray(5,5))
{
alert("Stop");
return false;
}
else
return true;
}

I used 5,5 to make as simple as possible. Am I wrong or shouldn't this throw the alert.

Any thoughts?

Thanks again.
 
Second function argument is array of values to search:
Code:
if( inArray( 5, [ 5, 6 ] ))
{  // this will trigger an alert because value 5 exists in array
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top