473,406 Members | 2,894 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,406 software developers and data experts.

Multiple 'If' statements - is there a better way to write this?

135 100+
Expand|Select|Wrap|Line Numbers
  1. var locationID=document.getElementById("locationID");
  2.  
  3.         if (locationID.value!="England")
  4.         if (locationID.value!="Wales")
  5.         if (locationID.value!="Ireland")
  6.         if (locationID.value!="Scotland")
  7.         {
  8.             alert("Please enter a location"); 
  9.             return false;
  10.         }
  11.  
Please use CODE tags - MODERATOR
Is there a better way to write this (see above code)? If you can start me start off that would be great.

cheers
Aug 13 '07 #1
3 1269
acoder
16,027 Expert Mod 8TB
Moved from Articles section.

Please post code using CODE tags.
Aug 13 '07 #2
acoder
16,027 Expert Mod 8TB
Is there a better way to write this (see above code)? If you can start me start off that would be great.
If locationID is select object, then just check if the value is equal to the empty string.
Aug 13 '07 #3
theS70RM
107 100+
yea what he said, i.e.:


Expand|Select|Wrap|Line Numbers
  1. var locationID=document.getElementById("locationID");
  2.  
  3. if (locationID.value == "") {
  4.   alert("Please enter a location");
  5.   return false;
  6. }
or if u need to check they havn't entered some other spurious location then:

Expand|Select|Wrap|Line Numbers
  1. function checklocation(){
  2.  
  3. var locationID=document.getElementById("locationID");
  4.  
  5. var allowed_locations = new Array("England", "Wales", "Ireland", "Scotland");
  6.  
  7.   for (i=0; i<=allowed_locations.length; i++){
  8.     if (locationID == allowed_locations[i])
  9.       return true;
  10.   }
  11. alert("enter a location");
  12. return false;
  13. }
be sure to check case sensitivity if you have let them enter this rather than using a dropdown box.
Aug 15 '07 #4

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

Similar topics

8
by: MrTrix | last post by:
Hello: I'm having a problem formulating the code to execute a multiple line command. I'm trying to execute something like: set rowcount 100000 declare @rowct int select @rowct = 1 while...
4
by: DG | last post by:
Hi, Can anyone advise how to execute multiple statements in a single query batch. For example- update customers set customer_name = 'Smith' where customer_name = 'Smyth'; select * from...
24
by: sureshjayaram | last post by:
In some functions where i need to return multiple error codes at multiple places, I use multiple return statements. Say for ex. if (Found == 1) { if (val == -1) return error1; } else { if...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
5
by: Arsen V. | last post by:
Hello, What is the optimal way to insert multiple rows (around 1000) from a web application into a table? The user enters multiple lines into a text box (up to 10,000). The ASP.NET...
5
by: mimo | last post by:
Hello, I have seen samples on how to pull data from one table and save back to it using the Form View control. How do I pull from multiple tables and save back to multiple tables on one...
9
by: TC | last post by:
I need to design a system which represents multiple "projects" in SQL Server. Each project has the same data model, but is independent of all others. My inclination is to use one database to store...
17
by: nergal | last post by:
What is a good programming style in C to handle multiple returns in a function that returns different values? - To have a variable that is set to the return value, which is in the end of the...
7
by: =?Utf-8?B?QVRT?= | last post by:
HOWTO Run multiple SQL statements from ASP/ADO to an Oracle 10g. Please help, I'm trying to write an ASP page to use ADO to run a long query against an Oracle 10g database, to create tables,...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.