Solved

Making a checkbox a required field.

Posted on 2002-06-11
6
160 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…

734 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