JavaScript Validate a checkbox
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 2 of 2

Thread: Validate a checkbox

  1. #1
    Join Date
    May 2006

    Validate a checkbox

    I have created a form that requires checking a box certifying that you agree to terms.

    <input name="certify" type="checkbox" value="Y" ID="CertChkbox"/>
    This is the last item on the form which contains mostly address info. I then added the following javascript:

    function validateForm()
    var x=document.forms["PledgeForm"]["FirstName"].value;
    if (x==null || x=="")
      alert("First name is a required field and must be completed");
    return false; 
    var x=document.forms["PledgeForm"]["LastName"].value;
    if (x==null || x=="")
      alert("Last name is a required field and must be completed");
    return false; 
    this goes on for a few more fields and then I tried adding the following code for  the checkbox:
    if (PledgeForm.Certify.value == null)
    	alert("error message");
    I am not sure what I am doing wrong. How can I validate the checkbox?

    Thanks in advance.
    Last edited by PeejAvery; December 1st, 2012 at 08:54 PM. Reason: Added code tags

  2. #2
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002

    Re: Validate a checkbox

    First off, never do client-side form processing if security is an issue at all. Anyone can disable or change JavaScript through their browser. The server-side scripts cannot be altered by the client.

    Second, if you're going to stick with JavaScript, checkboxes just return either true or false. It's as simple as that!

    if (document.forms["PledgeForm"]["Certify"].checked) {...}
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Posting Permissions

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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

This is a survey!

HTML5 Development Center