Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,471
There are 606 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     Javascript :

Help with JavaScript &&

Thread title: Help with JavaScript &&
Closed Thread    
    Thread tools Search this thread Display Modes  
10-08-2007, 02:01 PM
#1
Steven Fergus is offline Steven Fergus
Status: Hey, I'm Steven
Join date: Feb 2007
Location:
Expertise:
Software:
 
Posts: 1,212
iTrader: 3 / 100%
 

Steven Fergus is on a distinguished road

  Old  Help with JavaScript &&

Hi there,

I'm just learning JavaScript and I'm trying to validate a form. I have everything working except the part which should show if all fields are left empty. E.g.

Please enter your name
Please enter you email
Please enter your subject
Please choose a price option

I was using this code:

Code:
if ((name.value == "")&&(email.value == "")
   &&(subject.value == "")&&(Price_list.value == "")) {
           alert("Please enter your Surname \n Please enter a valid e-mail                                 address \n Please enter a Subject \n Please choose a price option");
              return false;
          }
Can && be used more than once to evaluate more than two expressions? (What I have above) or can it only be used once?

I have got it to work, using only

Code:
if ((name.value == "")&&(email.value == "")) {
           alert("Please enter your nam \n Please enter a valid e-mail                                 address");
              return false;
          }
So that made me wonder if I'm using && wrongly.

10-08-2007, 02:26 PM
#2
Steven Fergus is offline Steven Fergus
Status: Hey, I'm Steven
Join date: Feb 2007
Location:
Expertise:
Software:
 
Posts: 1,212
iTrader: 3 / 100%
 

Steven Fergus is on a distinguished road

  Old

My first question doesn't matter so much now. I've found a bigger problem with the code I'm using.

Here is the code I was trying to use to validate my form:

Code:
function validateForm()
      {
          with (document.order) {
          
          
          if ((name.value == "")&&(email.value == "")&&(subject.value == "")&&(Price_list.value == "")) {
              alert("Please enter your Surname \n Please enter a valid e-mail address \n Please enter a Subject \n Please choose a price option");
              return false;
          }
      
          if (name.value == "") {
              alert("Please enter your name");
              return false;
          }
   
           if (email.value == "") {
              alert("Please enter your email");
              return false;
          }
          
          if (subject.value == "") {
              alert("Please enter a Subject");
              return false;
          }
          
          if (Price_list.value == "") {
              alert("Please select a price option");
              return false;
          }
            }
        return true;
 
      }
But that wont work at all.



If I leave all the fields blank, I should - if the && statement worked - get a messege saying that all the fields need to be filled in. But, if I only fill in field A, it wont tell me to fill in B,C and D. It will only prompt me to fill in one of those three.

I'm sorry for going on, but it was hard to explain what I was meaning. I guess I need to look into a whole different method for doing this.

10-08-2007, 02:50 PM
#3
daz is offline daz
Status: I love this place
Join date: Jun 2005
Location: UK
Expertise:
Software:
 
Posts: 562
iTrader: 5 / 100%
 

daz is on a distinguished road

Send a message via MSN to daz

  Old

I don't know much about Javascript but I think you should use OR || e.g.

Code:
          if ((name.value == "")||(email.value == "")||(subject.value == "")||(Price_list.value == "")) {
              alert("Please enter your Surname \n Please enter a valid e-mail address \n Please enter a Subject \n Please choose a price option");
              return false;
          }

12-13-2007, 02:39 PM
#4
tristan is offline tristan
Status: I'm new around here
Join date: Dec 2007
Location:
Expertise:
Software:
 
Posts: 8
iTrader: 0 / 0%
 

tristan is on a distinguished road

  Old

Your && should be working.
You need to give each form field a name attribute. You should also give your form a name attribute. Then, to access your elements, you can say:
Code:
document.formName.elementName
//if your form was called "brokenForm" and you had an element with name "firstName" you could do:
if(document.brokenForm.firstName.value == "") {
//raise your error
}
Now, that said, I might go about this a little differently. My approach would be to initiate an error string at the top of the validation function and set it equal to "". Then, reset any highlighting you've done on the form fields (looks like you want to color them differently when theres an error). Analyze each field individually, and as you find errors, highlight that field as you'd like (by style changes) and add a line to the error string, separating each with a '<br/>' or <p> tags or something of the sort. At the end of the function, if error != "", you found an error somewhere. Populate your element with ID 'errors' (which you also happen to have set up with a red, bold style) with the error string, and return false. Otherwise, return true.


Hope you can make sense of that! Its early.....

Closed Thread    


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed