473,385 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to add 19 more states to ip block.

I'm using a web statistic service and I want to only allow certain states to my site as a testing mode for a certain period of time and block all non US countries.

I received the correct javascript below, but I can't figure out how to add the addtional states to the else statement. I tried everything. I don't know javascript. I need to add 19 more states to the else statement correctly. Can you give me the correct sequence to follow by adding 4 or five states as sampe to the else statement and letting me know how to end the statement correctly? The script below does work.

Expand|Select|Wrap|Line Numbers
  1. <script language="JavaScript">
  2. var geo = disp();
  3. if (geo[0] != 'US'){
  4.    alert('Sorry this site is only accessible from the USA');
  5.  
  6.    window.location = ('http://www.yahoo.com'); 
  7. }
  8. else {
  9.    if (geo[2] != 'CA' && geo[2] != 'TX' ){ (NEED TO ADD STATES HERE)
  10.       alert('Sorry this site is only accessible from California and Texas');
  11.  
  12.       window.location = ('http://www.yahoo.com'); 
  13.    }
  14. }
  15. </script>
Jan 11 '10 #1
6 1836
Dormilich
8,658 Expert Mod 8TB
I recommend a switch statement
Expand|Select|Wrap|Line Numbers
  1. switch (geo[2])
  2. {
  3.     case: "CA":
  4.     case: "TX":
  5.     case: "WA":
  6.     // …
  7.     case: "TN":
  8.         // execute "valid" code
  9.         break;
  10.     default:
  11.         alert(…);
  12.         location.href = "…";
  13. }
Jan 11 '10 #2
That was fast thanks.
Jan 11 '10 #3
Dormilich
8,658 Expert Mod 8TB
you get 5 extra dice if you’re a moderator ;)
Jan 11 '10 #4
gits
5,390 Expert Mod 4TB
another idea to shorten the code a bit would be to use a 'hash-map' like this:
Expand|Select|Wrap|Line Numbers
  1. var statesMap = { 'CA': 1, 'TX': 1, 'WA': 1, ... };
  2.  
  3. if ( geo[2] in statesMap) {
  4.     // valid handling
  5. } else {
  6.     // invalid handling
  7. }
  8.  
kind regards
Jan 11 '10 #5
Dormilich
8,658 Expert Mod 8TB
and just another one using arrays:
Expand|Select|Wrap|Line Numbers
  1. var statesMap = ["WA", "TX", "CA", …];
  2. if (-1 == statesMap.indexOf(geo[2]))
  3. {
  4.     // invalid handling
  5. }
  6. else
  7. {
  8.     // valid handling
  9. }
even searching in a string were possible …

you see, there are many ways to do this
Jan 11 '10 #6
Plater
7,872 Expert 4TB
Note: Poopy IE6 doesn't have the .indexOf() operator on its arrays.
Jan 11 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: TadPole | last post by:
Hi all, My main problems are::::::::: 1. Set a value within a block container that can be used and changed by subsequent templates/block-containers/tables etc.. 2. get/determine/find the...
1
by: S.Guhananth | last post by:
I need to capture a country from dropdown list and use that in sql query. How to do this protected DataSet GetCountry() { I have the code for this block
1
by: Novice Computer User | last post by:
Hi. I am looking to have a web page where the person first picks the State they are in (in the United States) which then brings them to a different page geared for their specific State. This can...
2
by: Vish | last post by:
Hi, I amplanning on having a rea-only and edit states for my form. But it do not want my form and its controls to look different or disabled. I am planning on having a edit button that brings...
6
by: Ethan V | last post by:
I have a few options regarding populating the state combo box 1. On page load, get the 50 states from the database 2. On application start, get the 50 states from the database and cache them in...
6
by: foolmelon | last post by:
If a childThread is in the middle of a catch block and handling an exception caught, the main thread calls childThread.Abort(). At that time a ThreadAbortException is thrown in the childThread. ...
11
by: akb | last post by:
can i write multiple object states of a serializable in a single file? if i can then how can i read back those states? i have no problem with writing multiple object states in a file. but i...
2
by: msdngroup | last post by:
Hi, All: Can you do something like cross-browser real-time message, remote JavaScript callback and monitoring browser states remotely? Can you ensure web browser running with super speed and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.