Monday, February 21, 2011

Matching form entries with javascript?

Hello,

I have a form with markup like this....

Which voucher did you cut out

<div class="answer item1">
  <input type="checkbox" name="downloaded_vouchers[answer1]" id="answer1" value="1"/>
   <label for="answer1">Answer 1</label>
</div>
<div class="answer item1">
   <input type="checkbox" name="downloaded_vouchers[answer1]" id="answer1" value="1"/>
   <label for="answer1">Answer 1</label>
</div>

Which voucher did you use

 <div class="answer item1">
  <input type="checkbox" name="used_vouchers[answer1]" id="usedanswer1" value="1"/>
   <label for="answer1">Answer 1</label>
</div>
<div class="answer item1">
   <input type="checkbox" name="used_vouchers[answer1]" id="usedanswer1" value="1"/>
   <label for="answer1">Answer 1</label>
</div>

I need to be able to somehow, on submition of the form check that the voucher cut matches the one that was used, there is some validation server side but I need to do some client side as well and have no idea where to start, I need to use straight javascript no jquery or similar, can anyone help?

From stackoverflow
  • something like this....

       <script type="text/javascript">
        function validate( )
        {
            // use document.getElementById to get the form item
            var item = document.getElementById("yourElement");
            if ( somecondition == true )
            {
                alert("good to go");
                // allow the form to post
                return true;
            }
            else
            {
                alert("I don't think so");
                // return false so the form will not post
                return false;
            }
        }
        </script>
    
        <form onsubmit="Validate();" >
        </form>
    

    returning true from the onsubmit method will send the form through, returning false will not

0 comments:

Post a Comment