subha rr Posted April 18, 2009 Share Posted April 18, 2009 Hi.....how do I check if a value is in an arrayvar arr = new Array('.com','.net','.org','.biz','.coop','.info','.museum','.name','.pro' ,'.edu','.gov','.int','.mil','.ac','.ad','.ae','.af','.ag','.ai','.al'...........);RegardsSubha Link to comment Share on other sites More sharing options...
jeffman Posted April 18, 2009 Share Posted April 18, 2009 The normal technique for that kind of validation involves regular expressions. But there's nothing wrong with what you're trying to do.Here's a nifty way of adding a method to the Array object. It does what you want, and you'll find 1001 uses for it besides. Array.prototype.contains = function (val) { for (var sub in this) { if (this[sub] == val) { return true; } } return false;}// USE IT LIKE THISvar arr = ['Sam','Abdul','Grete'];var person = "Abdul";if (arr.contains(person) ) { alert ("YES");} Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.