Avatar of Aleks
AleksFlag for United States of America

asked on 

Update function to validate checkbox is checked

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

HTMLASPjQueryJavaScript

Avatar of undefined
Last Comment
Aleks
Avatar of James Bilous
James Bilous
Flag of United States of America image

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.
Avatar of Aleks
Aleks
Flag of United States of America image

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of James Bilous
James Bilous
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Aleks
Aleks
Flag of United States of America image

ASKER

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

JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo