Solved

Update function to validate checkbox is checked

Posted on 2016-08-17
4
100 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
[X]
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
  • 2
  • 2
4 Comments
 
LVL 9

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 9

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article discusses how to create an extensible mechanism for linked drop downs.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

739 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