Solved

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

Posted on 2011-03-09
8
411 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
DevOps Toolchain Recommendations

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

 
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

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

777 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