Solved

Update function to validate checkbox is checked

Posted on 2016-08-17
4
57 Views
Last Modified: 2016-08-17
I have a function that is triggered by the click of a button. It validates that at least one checkbox is checked in the form. The problem is that the form has 2 checkboxes, one I do NOT need validated, but it is validating both apparently or validating the incorrect box.

This is my button
<button type="submit" class="btn btn-white" formaction="links_spunlinkusers.asp?caseid=<%=(rs_case.Fields.Item("Id").Value)%>"  onclick="return oneCheckboxChecked(form1)"> Unlink </button>

Open in new window


This is the checkbox I NEED validated that at least one is checked
<input  name="box" id = "box" type="checkbox" value="<%=(rs_users.Fields.Item("userid").Value)%>" >

Open in new window


This is the checkbox I DO NOT need validated (this may or not be checked)
 <input class="i-checks" <%If (CStr((rs_users.Fields.Item("Xfer2Forms").Value)) = CStr("True")) Then Response.Write("checked") : Response.Write("")%> name="<%= (iCount & ".Qty") %>" type="checkbox" value="1">

Open in new window


This is my function. Which I think I need to update so that only validates the checkbox named 'box'

How can the function be modified to only validate that element?

<script language="JavaScript">
function oneCheckboxChecked(form){
for(f=0;f<form.length;f++){
if(form[f].type !="checkbox") continue;
if(form[f].checked){
return true;
}
}
alert("Select at least one User");{
return false;
}
}
</script>

Open in new window

0
Comment
Question by:amucinobluedot
  • 2
  • 2
4 Comments
 
LVL 7

Expert Comment

by:James Bilous
ID: 41759930
In oneCheckBoxChecked, if the conditions are not met for form submission, return false which will prevent the default event. Also, remove the "return" from the button onclick call.
0
 

Author Comment

by:amucinobluedot
ID: 41759931
James, sorry I was updating the question, please read above. I need to have the function indicate which of the checkbox elements inside the form should be validated, I have two checkboxes but only need the one named 'box' validated and not the other one.
0
 
LVL 7

Accepted Solution

by:
James Bilous earned 500 total points
ID: 41760019
You can check if(form[f].getAttribute("name") == "box") to validate only checkboxes with the name box, otherwise continue
1
 

Author Comment

by:amucinobluedot
ID: 41760119
I added it like this and seems to work.

function oneCheckboxChecked(form){
for(f=0;f<form.length;f++){
if(form[f].type !="checkbox") continue;
if(form[f].getAttribute("name") == "box")
if(form[f].checked){
return true;
}
}
alert("Select at least one User");{
return false;
}
}

Open in new window

0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

746 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

9 Experts available now in Live!

Get 1:1 Help Now