Solved

Making a checkbox a required field.

Posted on 2002-06-11
6
158 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
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to use embargo Date/time in php 29 39
How to make footer stick to bottom 9 31
Html test in IIS 4 20
Html form and modal / img src -problem 3 11
Introduction The Google Maps API offers various ways to draw dynamic and static maps.  Using a combination of PHP and JavaScript, you can draw active JavaScript maps that allow pan-and-zoom in the client browser window.  You can also draw "static" …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…

809 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