Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Feb 2004
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Form validates regardless of boolean value! :)

    Hi Everyone,

    I am attempting to validate a form before forwarding it to the server. However my server side code is being called regardless of the boolean value that my javascript returns.

    My form markup looks like so

    <form action="CraftSurvey" id="surveyform" method="post" name="surveyform" onsubmit="checkVote();">
    <p align="left">
    <input type="radio" name="surveybutton" value="Bears">

    <span class="black_verdana_text">Bears
    </span></p>
    <p align="left">
    <input type="radio" name="surveybutton" value="Country & Garden">
    <span class="black_verdana_text">Country & Garden</span>
    </p>
    <p align="left">
    <input type="radio" name="surveybutton" value="Floral">
    <span class="black_verdana_text">Floral
    </span></p>

    <p align="left">
    <input type="radio" name="surveybutton" value="Hearts">
    <span class="black_verdana_text">Hearts</span>
    </p>
    <p align="left">
    <input type="radio" name="surveybutton" value="Wording">
    <span class="black_verdana_text">Wording</span>
    </p>
    <p align="center">
    <input name="surveyvote" type="submit" class="black_verdana_text" value="Vote Now">

    </p>
    </form>

    And my form validation script looks like so:

    <script type="text/JavaScript">
    //<!--
    function checkVote()
    {
    var valid = false;

    var bears = document.surveyform.surveybutton[0];
    var country = document.surveyform.surveybutton[1];
    var floral = document.surveyform.surveybutton[2];
    var hearts = document.surveyform.surveybutton[3];
    var wording = document.surveyform.surveybutton[4];

    if((bears.checked == false) && (country.checked == false) && (floral.checked == false) && (hearts.checked == false) && (wording.checked == false))
    {
    alert("Please select a category before voting.");
    }
    else
    {
    valid = true;
    }

    return valid;
    }
    //-->
    </script>

    The script is inside the head section of the html document.

    If anybody has any suggestions regarding the cause of this challenge they will be greatly appreciated.

    Thanks

    Davo

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    You forgot to use the return conditional

    <form action="CraftSurvey" id="surveyform" method="post" name="surveyform" onsubmit="return checkVote();">
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

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