Solved

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

Posted on 2011-03-09
8
409 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
Comment Utility
0
 
LVL 8

Expert Comment

by:vsudip
Comment Utility
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
Comment Utility
I would recommend using jQuery library for that:
http://docs.jquery.com/Plugins/validation
does most of the work you want, for you.
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:cuconsortium
Comment Utility
This question is still active.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
What does that mean.

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

Author Comment

by:cuconsortium
Comment Utility
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
Comment Utility
Thank you all your patient~~
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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…

772 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

11 Experts available now in Live!

Get 1:1 Help Now