0

I am experimenting with a form that has a text field and a check box. I am a designer and not a developer. I have successfully gotten the text box to Validate, but I have pasted in code for the checkbox, which for some reason either cancels out the validation or returns an error at the paypal server. Can anyone suggest a few lines of code to validate the checkbox "terms"? The textbox is called "os0"

    <form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top" id="myForm" onSubmit="return validateForm()" >
        <input type="hidden" name="cmd" value="_s-xclick">
        <input type="hidden" name="hosted_button_id" value="AXNKKDCZLVHM4">
        <table width="100%" border="0">
            <tr>
                <th scope="row"><input type="hidden" name="on0" value="Your RCEH Account Number:" />
                    Your RCEH Account Number:
                </th>
                <td>
                    <input type="text" name="os0" maxlength="200" />
                </td>
            </tr>
            <tr>
                <th scope="row">&nbsp;</th>
                <td><label>
                    <input type="checkbox" name="terms" id="terms" />
                    Agree to <a href="../terms.html" target="new">terms and conditions</a></label>
                </td>
                </tr>
        </table>
        <input type=submit value="Proceed to secure server">
        <img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
    </form>
 </div>
</div>

<script>
    function validateForm() {
        var x=document.forms["myForm"]["os0"].value;
        if (x==null || x==""){
            alert("RCEH account number must be filled out");
            return false;
        }
    }
</script>
2
  • which type of validation you want to do on checkbox? Commented May 25, 2013 at 10:54
  • Thanks for getting back to me. LightStyle solved it for me. Commented May 25, 2013 at 11:07

1 Answer 1

2

Assuming that you only want your checkbox to be checked, below there is the code you need, and this is a working JSFiddle.

function validateForm() {
    var form = document.getElementById("myForm"); //get the form element via ID
    if (form.os0.value == "")
    {
        alert("RCEH account number must be filled out");
        return false;
    }
    if (!form.terms.checked) { // checks if the checkbox called "terms" is checked; if it is not, alerts a message and return false
        alert("Terms must be read and approved");
        return false;
    }
    return true; //if everything is okay, return true and submit the form
}
Sign up to request clarification or add additional context in comments.

1 Comment

Light Style that worked a charm! Much appreciated! I had tried pasting a few different lines from the internet. I was confused, some posts had multiple 'var' lines. Again, much appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.