Solved

Making a checkbox a required field.

Posted on 2002-06-11
6
155 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Do you want to insert HTML5 video into your site? This is the tutorial how to do so. What are the main advantages of HTML5 video? 1) Have good compression, good image quality, and low decode processor use. 2) It is royalty-free 3) It is easi…
Someone recently asked me about how to display a progress indicator on a page while an iframe is loading. And I remember when I first came across this myself. It was a bit tricky to get my head around, but really, it's very simple. The most impor…
In this tutorial viewers will learn how add a full-size background image to a webpage using CSS3. Create a new HTML document with an internal stylesheet.: In CSS, define the html element to have a background image. Use a high resolution image.: In t…
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now