Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Update function to validate checkbox is checked

Posted on 2016-08-17
4
81 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 8

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 8

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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 position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

765 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