• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 164
  • Last Modified:

Making a checkbox a required field.

I would like to know if there is a way to make a checkbox on a form a required field. I know that normally a checkbox is used to pick an option/selection but I would like to use it as a way to control accountability.

i.e. By selecting this box you agree to XXX....

Thank you,
seamil
0
seamil
Asked:
seamil
  • 4
  • 2
1 Solution
 
knightEknightCommented:
<FORM name='myform' action='whatever' method='post'
   onSubmit='return this.mycheckbox.checked;'>

  <INPUT type='checkbox' name='mycheckbox'>

</form>
0
 
knightEknightCommented:
the above code will prevent the form from submitting if the checkbox is not checked.
0
 
knightEknightCommented:
also, you may want to present a message:

<SCRIPT language='javascript'>
 function validate(theForm)
 {
    var c = theForm.mycheckbox.checked;
    if ( c == false ) alert("you must check the box!");
    return c;
 }
</script>


<FORM name='myform' action='whatever.asp' method='post'
      onSubmit='return validate(this);'>

  <INPUT type='checkbox' name='mycheckbox'>

</form>
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
seamilAuthor Commented:
knightEknight,

The onSubmit works fine but I can not get the message to appear with the script you provided.

I modified the (theForm) to match the name I gave my form and mycheckbox to match the name of my checkbox but no message appears.

seamil
0
 
knightEknightCommented:
you can call it whatever you want inside the function as long as you pass a reference to the form when you call the function.  So you can do this:

    function validate(theForm)

and use "theForm" in the function, as long as you do this in the form tag:

    onSubmit='return validate(this);'>

which is another way of doing this:

    onSubmit='return validate(document.formname);'>

If you still have problems, post your code and I will look it over.

0
 
seamilAuthor Commented:
It works!

Thank you very much!

Seamil
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now