www.webdeveloper.com
Results 1 to 3 of 3

Thread: Problem with form validation code

  1. #1
    Join Date
    Nov 2002
    Location
    Iowa
    Posts
    2

    Problem with form validation code

    I am fairly new to using Javascript and need some help figuring out what I am doing wrong with the following script.

    ------------------------------------------

    <SCRIPT LANGUAGE="JavaScript">
    <!-- Begin
    function checkEntrys() {
    if (!order1.Breed.value) {
    alert("Please enter the breed.");
    order1.Breed.focus();
    return false;
    }
    if (!order1.Reg.value) {
    alert("Please enter the registration type.");
    order1.Reg.focus();
    return false;
    }
    if (order1.Reg.value != "PUR") {
    if (!order1.Regnumb.value)
    alert("Please enter the registration number.");
    order1.Regnumb.focus();
    return false;
    }
    return true;
    }
    // End -->
    </script>

    ------------------------------------------

    I run this code when a submit button is clicked. The "Breed" and "Reg" checks work fine but I run into a problem with the last check when the "Reg" value is something other that "PUR".

    What I want to happen is when the "Reg" value is "PUR" I want the page to be submitted. When the "Reg" value is something other that "PUR" I want to make sure the "Regnumb" value is not null.

    The submit button works when the "Reg" value is "PUR", but when it is another value the 'return true' is never gotten to and the page remains displayed and does not bring up the next page like it should.

    I would appreciate some help with how I have coded this script. It is probably something simple, so please be kind and don't burn me too bad.

    Thanks
    Dogger

  2. #2
    Join Date
    Nov 2002
    Location
    Columbia, MD
    Posts
    14

    Lightbulb

    It looks to me like you need an Else in there somewhere:

    Code:
    if (order1.Reg.value != "PUR")
    { 
      if (!order1.Regnumb.value) 
      {
        alert("Please enter the registration number."); 
        order1.Regnumb.focus(); 
        return false; 
      }
      else
      {
        return true;
      }
    }
    else
    }
      return true; 
    }
    Hope this helps. Good luck!

  3. #3
    Join Date
    Nov 2002
    Location
    Iowa
    Posts
    2
    You were right. The 'else' made the difference.

    Thanks
    dogger

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles