Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What's wrong with this if statement?

Status
Not open for further replies.

mnongkhlaw

Programmer
Feb 22, 2002
62
IN
I want to detect if dates entered by users in a textbox contains JAN,FEB,MAR etc :

Code:
var goodDate = true;
var theChar = theInput.substring(2,5); //see theInput below
if (theChar != "JAN" || theChar != "FEB" || theChar != "MAR" || theChar != "APR" || theChar != "MAY" || theChar != "JUN" || theChar != "JUL" || theChar != "AUG" || theChar != "SEP" || theChar != "OCT" || theChar != "NOV" || theChar != "DEC"){

goodDate = false;
alert("theChar= "+theChar);
alert("goodDate= "+goodDate);
}

For example, when theInput="1/OCT/99" the first alertbox gives me theChar=OCT, but the second alertbox still gives me goodDate=false when I expect it to be true.

What's wrong with the above code?

Mark
 
your origianl if statement mean: if theChar equal to any of Month, set goodDate = false. i.e. if a user enter a valid month set goodDate to false. Doesn't sound right!

Change logical OR to AND in the if statement.

// if theChar is not equal any of these
if (theChar != "JAN" && theChar != "FEB" && theChar != "MAR" && theChar != "APR" && theChar != "MAY" && theChar != "JUN" && theChar != "JUL" && theChar != "AUG" && theChar != "SEP" && theChar != "OCT" && theChar != "NOV" && theChar != "DEC"){

// then set goodDate to false as not matching any month.

or

no need to change the if statement, simply change default goodDate valueto false

var goodDate = false;

then inside if statement block, change

goodDate = true;

 
byam,

I'm getting confused here : Doesn't != mean NOT equal to?

I changed || to && but I still get the same results!
&& doesn't sound right to me, anyway.

Mark
 
byam,

I solved the problem by replacing != with == and setting goodDate=true

Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top