Welcome to WebmasterWorld Guest from 54.196.217.43

Forum Moderators: open

Message Too Old, No Replies

Check to see if variable equals approved values

     
2:06 am on Jun 6, 2012 (gmt 0)

Full Member

5+ Year Member Top Contributors Of The Month

joined:Sept 30, 2009
posts:222
votes: 0


I'm trying to set a variable equal to the value of an url parameter. If this value does not match one of the approved values then I need to set this variable to a default value.

I'm using or statements right now, but this gets kind of ridiculous when you have many different approved values. Is there some kind of shorthand or better way of doing this?


var lang = [grab value of lang parameter if set];

if(lang!="en" || lang!="es" || lang!="pt" || lang!="ja" || lang!="zh" || ...) lang = "ko";
3:37 am on June 6, 2012 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts: 4965
votes: 10


You could use a switch statement:

var lang = [grab value of lang parameter if set];
switch (lang) {
case 'en':
case 'es':
case 'pt':
case 'ja':
case 'zh':
lang = 'ko';
break;
default:
lang = 'XX';
break;
}