Jump to content

Javascript AND operator Issues

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
millsy007

millsy007

    Newbie

  • Members
  • PipPip
  • 15 posts
Hi
I am trying to run some code that will check if the user enters two values from 'depart' and 'arrival' select lists that would make up an invalid journey:

   var cdate, ctime, cdepart, carrive, cname;

   with(window.document.example)

   {

      

	  cdate = date;

	  ctime = time;

	  cdepart = depart;

	  carrive = arrive;

	  cname = name;

	  

   }


   if(trim(cdepart.value) == 'Uptown' && trim(carrive.value) == 'Downtown')

   {

      alert('invalid journey');

      cdepart.focus();

      return false;

   }


   function trim(str)

   {

    return str.replace(/^\s+|\s+$/g,'');

   }

However when I select the values the alert is not shown? I think My syntax is okay, maybe not?

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I don't see that your if statement is contained in a function. You need it to react to an event, which means placing it in a function.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
millsy007

millsy007

    Newbie

  • Members
  • PipPip
  • 15 posts
Hi I only posted snippets of my code, the function works as other validation checks work, it is only where I use the && check that it is not working?

I tried with extra brackets but still doesnt work
if ((trim(cdepart.value) == 'Uptown') && (trim(carrive.value) == 'Downtown'))

?

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I would toss in an else to display their values in cdepart and carrive with an alert.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
millsy007

millsy007

    Newbie

  • Members
  • PipPip
  • 15 posts
Thanks, but dont quite understand, I know that the values are what I expect as when I check on them individually the checks work. It is only when I try the check in combination using && I get a problem.

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
    alert(cdepart.value);
    alert(carrive.value);
    if(trim(cdepart.value) == 'Uptown' && trim(carrive.value) == 'Downtown')
   {
      alert('invalid journey');
      cdepart.focus();
      return false;
   }

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
millsy007

millsy007

    Newbie

  • Members
  • PipPip
  • 15 posts
Thanks, your alert suggestion helped, I didnt realise the values were the numeric values, when I did

   if(trim(ctime.value) == 'Select Time')

   {

      alert('Enter a Time');

      ctime.focus();

      return false;

   }

It worked checking the text, but when I used the && I had to compare the numerical values:

    if(trim(cdepart.value) == '5' && trim(carrive.value) == '4')

   {

      alert('Invalid Journey');

      cdepart.focus();

      return false;

   }