Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Update function to validate checkbox is checked

Posted on 2016-08-17
4
Medium Priority
?
118 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 2000 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

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 article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

609 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