Solved

Checking a Drop Down Value against a Javascript Numeric Array List

Posted on 2010-09-16
3
407 Views
Last Modified: 2013-11-18
I would like to check an drop down list value on a form page with a numeric list to see if the value is in the list.
How can this be done via Javascript?

For example:
 
var myList = new Array(2,3,14,25); // the id list to check against

var fieldVal = thing[thing.selectedIndex].value; // the numeric selected drop down field value


if(fieldVal == myList[idx])
    alert("Yes the drop down value is in my array list");
else
    alert("No. the drop down value is not in the array list");

0
Comment
Question by:FastEddie___
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 40

Accepted Solution

by:
gurvinder372 earned 500 total points
ID: 33695544
Array.prototype.contains = function(obj) {
    var i = this.length;
    while (i--) {
        if (this[i] == obj) {
            return true;
        }
    }
    return false;
}

var myList = new Array(2,3,14,25); // the id list to check against
var fieldVal = parseInt( thing[thing.selectedIndex].value );

if( myList.contains(fieldVal) )
    alert("Yes the drop down value is in my array list");
else
    alert("No. the drop down value is not in the array list");
0
 
LVL 2

Expert Comment

by:agileblowfish
ID: 33695557

for (var i=0; i < myList.length; i++) {
	if (fieldVal == myList[i]) {
		alert("Yes the drop down value is in my array list");
	}else{
		alert("No. the drop down value is not in the array list");
	}
}

Open in new window

0
 

Author Closing Comment

by:FastEddie___
ID: 33697130
Thanks
0

Featured Post

Industry Leaders: 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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
This article discusses how to implement server side field validation and display customized error messages to the client.
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

635 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