Solved

Making a checkbox a required field.

Posted on 2002-06-11
6
159 Views
Last Modified: 2010-04-09
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
Comment
Question by:seamil
  • 4
  • 2
6 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 7071248
<FORM name='myform' action='whatever' method='post'
   onSubmit='return this.mycheckbox.checked;'>

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

</form>
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 7071250
the above code will prevent the form from submitting if the checkbox is not checked.
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 7071257
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
Industry Leaders: 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!

 

Author Comment

by:seamil
ID: 7071316
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
 
LVL 33

Accepted Solution

by:
knightEknight earned 100 total points
ID: 7071326
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
 

Author Comment

by:seamil
ID: 7071355
It works!

Thank you very much!

Seamil
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question