|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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 |
|
#3
|
||||
|
||||
|
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 |
|
#4
|
|||
|
|||
|
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 ![]() |
![]() |
| Viewing: Dev Articles Community Forums > Programming > JavaScript Development > JavaScript Form help |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|