SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: JavaScript Help

  1. #1
    SitePoint Member
    Join Date
    Nov 2010
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question JavaScript Help

    Hi Everyone,

    Im a student studying Web Development/Design and I am having an issue with some JavaScript for one of my assignments.

    I am having trouble validating a form on submit. Everything validate nicely except for the selection list. Below is the script I have for the selection list so far:

    if((!document.clientfeedback.staff.selectedIndex==0))
    {
    alert("You must choose the number of staff");
    document.clientfeedback.staff.focus();
    return false;
    }

    Any help would be greatly appreciated.

    Cheers Peta

  2. #2
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try removing the !

    Code:
     
    if((document.clientfeedback.staff.selectedIndex==0))
    {
          alert("You must choose the number of staff");
          document.clientfeedback.staff.focus();
          return false;
    }

  3. #3
    SitePoint Member
    Join Date
    Nov 2010
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you so much! That works now.

    Why would removing the '!' make it work? Some of the other validations have the '!'

    Quote Originally Posted by Kalon View Post
    try removing the !

    Code:
     
    if((document.clientfeedback.staff.selectedIndex==0))
    {
          alert("You must choose the number of staff");
          document.clientfeedback.staff.focus();
          return false;
    }

  4. #4
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the ! means "not" and so

    Code:
     
    if(!document.clientfeedback.staff.selectedIndex==0)
    means "if the selectedIndesx is not equal to 0".

    it is the same as

    Code:
     
    if(document.clientfeedback.staff.selectedIndex != 0 )

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by peta.mackenzie View Post
    Thank you so much! That works now.

    Why would removing the '!' make it work? Some of the other validations have the '!'
    The ! means NOT. You'll also see && which means AND and || which means OR

    This code says:

    Code javascript:
    if (!(document.clientfeedback.staff.selectedIndex == 0)) {

    If it's NOT the case that the selected index is 0.

    This part equates to TRUE
    document.clientfeedback.staff.selectedIndex == 0

    And the ! inverts that, to mean FALSE.
    !(TRUE) is FALSE

    You however, want the condition to be true when the selected index is zero instead.

    Code javascript:
    if(document.clientfeedback.staff.selectedIndex == 0) {
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  6. #6
    SitePoint Member
    Join Date
    Nov 2010
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks everyone for your replies.

    Im slowing learning JavaScript and this helps me understand it better

  7. #7
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    in case you are not aware, one of many very good resources on the www for javascript is the w3schools javascript tutorials

  8. #8
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Edit:

    Thread split off to Is w3schools now out of date?
    Please post replies to this post over there.


    Quote Originally Posted by Kalon View Post
    in case you are not aware, one of many very good resources on the www for javascript is the w3schools javascript tutorials
    Those tutorials are getting out of date.

    Just looking at one example of submitting a form, the problems are:

    • inline event attributes and inline scripting are used, whereas traditional scripting events (or even advanced ones) are now the favored technique
    • Script is in the head, whereas we now put our scripts at the bottom
    • A button is used to trigger the script to submit the form, whereas a standard submit button should be used instead


    In fact, their example code can be completely replaced with HTML only code, no scripting. Their scripting example provide no benefit over standard techniques.

    Actually, their scripted form example completely fails to work at all when scripting is not available. This surprizingly means that an HTML-only solution is superior in practice to their script coded example. Not a good look.

    Most of their other code examples face similar "out of date" problems too.

    w3schools used to be a good resource, but it has long since been surpassed by the passage of time and circumstance.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •