JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today!
  #1  
Old April 14th, 2004, 12:16 PM
rme8494 rme8494 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 1 rme8494 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Exclamation JavaScript Form help

I'm trying to validate a form. In this simplistic example I just want to check to see if the form value of Entity is either "Hi" or "Bye", however the code I have always returns false. If I take out the || 'bye' part of the code it works fine. How can I add multiple values?

<script>
function Form_Validator(theForm)
{
formname=document.GUI8522;
if (formname.EntityCode.value == "")
{
alert("You are required to type in an entity code");
formname.EntityCode.focus();
return (false);
}
else
{
if (formname.EntityCode.value != 'hi' || 'bye')
{
alert(formname.EntityCode.value + " is an" + " invalid entity code");
formname.EntityCode.focus();
return (false);
}
}
}
</script>

Thanks
Ryan

Reply With Quote
  #2  
Old April 15th, 2004, 08:38 AM
masood masood is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 8 masood User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Your statment:

......|| 'bye')


Should look like this:

..... || formname.EntityCode.value != 'bye')


Also, consider using the statment:

else if (...) {}

rather then another if statment within the else clause.

Last edited by masood : April 15th, 2004 at 08:40 AM. Reason: typo

Reply With Quote
  #3  
Old April 15th, 2004, 11:57 PM
stumpy's Avatar
stumpy stumpy is offline
May contain nuts.
Dev Articles Regular (2000 - 2499 posts)
 
Join Date: Aug 2002
Location: Sydney, AU
Posts: 2,058 stumpy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 6 m 11 sec
Reputation Power: 8
Send a message via ICQ to stumpy Send a message via MSN to stumpy
If all you are doing is checking for various values for one field, it is better to use a case (switch) statement, rather than multiple if,elses...

http://devedge.netscape.com/library...mt.html#1018610
__________________
DevArticles Moderator
BlueSix - Web Development and Consulting

Reply With Quote
  #4  
Old April 16th, 2004, 11:36 AM
masood masood is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 8 masood User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Now that I think of it, I think its actually illogical to expect anything but a true result when comparing two statments with != || !=

for instance

Code:
 
var str1 = "hello"; 
if (str1 != "hello" || str1 != "yikes")
  {
  alert("this will always execute");
  }



no matter what the value of str1 is, one of the != statements will evaluate to true, and consequently execute your code block.

The switch statement is the best option if you have many values you are checking for... if it's just one or two, doesn't really matter.

Good call on the switch stumpy

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJavaScript Development > JavaScript Form help


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway