Solved

Java Script: fire a function when Check Box value = Yes

Posted on 2011-03-09
8
412 Views
Last Modified: 2012-05-11
Dear all,

  I created a Java Script function to validate input fields of a Form.  Since the form is long, instead of validate the form on Sumit, I'd like to have the page validate the input fields when a user checks one of the Check Box on the form.  Since check box changes value when clicked and unclicked, how do I make the page validates the form when the Check Box is Checked?

<html>
<head>
<script>

  function validateForm()

    { bluh bluh bluh... }

</script>
</head>


<body>

<form>

bluh..
bluh..

<input type="checkbox" name="chkbox_filled" value="Yes"> Please Validate this Section

</form>
</body>  
</html>
0
Comment
Question by:cuconsortium
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 8

Expert Comment

by:vsudip
ID: 35091931
0
 
LVL 8

Expert Comment

by:vsudip
ID: 35091949
u can also refer below code....
Source: http://www.java2s.com/Tutorial/JavaScript/0200__Form/Checkboxclick.htm
<html>
    <form name="orderForm">
      <input type="checkbox" name="door4" onClick="alert('door4')">4 doors<br>
      <input type="checkbox" name="door2" onClick="displayNote()">2 doors<hr>
      Step 2:
      <input type="button" value="Submit Order" name="orderButton" onClick="alert('submitted')">
    </form>
    <script language="JavaScript">
    <!--
    function displayNote(){
      alert("Have you considered the 4-door version?");
      document.orderForm.door4.click();
    }
    -->
    </script>
    </html>

Open in new window

0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35091955
I would recommend using jQuery library for that:
http://docs.jquery.com/Plugins/validation
does most of the work you want, for you.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 35092722
here is what I would do
Notice the (this) and (this.form)
<html>
<head>
<script>

  function validateForm(theForm) { // pass the form to the function

    bluh bluh bluh...
    return false; // cancel submission

    bluh bluh bluh...

    return true; // allow the submission
  }

</script>
</head>


<body>

<form onsubmit="return validateForm(this)">

bluh..
bluh..

<input type="checkbox" name="chkbox_filled" value="Yes" onClick="if (this.checked) validateForm(this.form)"> Please Validate this Section

</form>
</body>  
</html>

Open in new window

0
 

Author Comment

by:cuconsortium
ID: 35386257
This question is still active.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 35387190
What does that mean.

Can you use what I posted? If not why not?
0
 

Author Comment

by:cuconsortium
ID: 35388240
I was just been traveling.  now, I'm back and am able to get back to this for the following 2 weeks for sure! sorry for the delay.
0
 

Author Comment

by:cuconsortium
ID: 35708019
Thank you all your patient~~
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Suggested Solutions

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

837 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