Validation of Checkbox in Listview Control by Jquery/Javascript? - javascript

I am trying to validate the checkbox which is one of the items in ListView control.
I have the button to check so I define the ClientClick function on the button and wrote JavaScript code.
But It didn't work. Display the lstViewtest object null.
function btnclick() {
var listview = document.getElementById('<%=
lstviewtest.FindControl("tableItem").ClientID %>');
for (var i = 0; i < listview.rows.length; i++) {
var inputs = listview.rows[i].getElementsByTagName('input');
for (var j = 0; j < inputs.length; j++) {
if (inputs[j].type === "checkbox" || inputs[j].type === "checkboxsend")
if (inputs[j].checked)
return true;
}
alert("Please select at least one");
return false;
}
}

Related

I am not able to set the Global variable in the following code. What am i doing wrong?

Here is the issue: I am trying check and uncheck the check boxes by a group.
So if you select G1 and G2, it throws a message that you cant mix groups. If you uncheck all of them, i am trying to clear the existing grouping and that is where the code seems to fail.
Any thoughts? (also,i might be having a wrong idea about that global var at the beginning. so please suggest)
<HTML>
<script language="javascript">
var prodSel="";
function VerifyGroup(a,b)
{
ClearAllSelectionsA(); // check if this is the last unselect and clear the prodSel variable
if (prodSel == "")
{
prodSel = a;
}else
{
if (prodSel != a)
{
alert ( "Please ensure that the groups are same for the items you select");
//alert(b);
document.getElementById(b).checked = false;
}
}
}
function ClearAllSelections()
{
var inputs = document.getElementsByTagName("input");
var cbs = [];
for (var i = 0; i < inputs.length; i++)
{
if (inputs[i].type == "checkbox")
{
inputs[i].checked = false;
}
}
prodSel=""; // Clear the variable; allow new selections
}
/*loop through and if all of them are unchecke,d clear the variable*/
function ClearAllSelectionsA()
{
var clre = true;
var inputs = document.getElementsByTagName("input");
var cbs = [];
for (var i = 0; i < inputs.length; i++)
{
if (inputs[i].type == "checkbox")
{
if (inputs[i].checked){clre= false;}
}
}
if (clre){
prodSel=""; // Clear the variable; allow new selections
alert(window.prodSel);
}
}
</script>
<body>
G1<input type="checkbox" value="zxc" id="12" onclick="javascript:VerifyGroup('g1',12);"><BR>
G1<input type="checkbox" value="zxcw" id="123" onclick="javascript:VerifyGroup('g1',123);"><BR>
G1<input type="checkbox" value="zxcdw" id="124" onclick="javascript:VerifyGroup('g1',124);"><BR>
G2<input type="checkbox" value="zxcf" id="125" onclick="javascript:VerifyGroup('g2',125);"><BR>
G2<input type="checkbox" value="zxcfg" id="126" onclick="javascript:VerifyGroup('g2',126);"><BR>
clear group
</body>
</html>
When you call ClearAllSelectionsA, if all the checkboxes are unchecked, then prodSel gets cleared. Then back in VerifyGroup, prodSel is immediately being reassigned to a. My recommendation would be to return true or false from ClearAllSelectionsA and act based upon that value.
<script language="javascript">
var prodSel="";
function VerifyGroup(a,b)
{
var cleared = ClearAllSelectionsA(); // check if this is the last unselect and clear the prodSel variable
if (prodSel == "" && !cleared) //only reset prodSel if there is a checkbox checked
{
prodSel = a;
}else
{
if (prodSel != a)
{
alert ( "Please ensure that the groups are same for the items you select");
//alert(b);
document.getElementById(b).checked = false;
}
}
}
function ClearAllSelections()
{
var inputs = document.getElementsByTagName("input");
var cbs = [];
for (var i = 0; i < inputs.length; i++)
{
if (inputs[i].type == "checkbox")
{
inputs[i].checked = false;
}
}
prodSel=""; // Clear the variable; allow new selections
}
/*loop through and if all of them are unchecke,d clear the variable*/
function ClearAllSelectionsA()
{
var clre = true;
var inputs = document.getElementsByTagName("input");
var cbs = [];
for (var i = 0; i < inputs.length; i++)
{
if (inputs[i].type == "checkbox")
{
if (inputs[i].checked){clre= false;}
}
}
if (clre){
prodSel=""; // Clear the variable; allow new selections
alert(window.prodSel);
}
return clre;
}
</script>

asp.net mvc checkbox uncheck on check of toher

I have an MVC 2 view with two checkboxes like this:
<%: Html.Label("Is Customer")%>
<%
accountStatus = Model.AccountStatus == "Customer" ? true : false;
%>
<%: Html.CheckBox("IsCustomer", accountStatus.Value, new { onclick = "uncheckAll(this);" })%>
I want that if one checkbox is checked then other one is uncheked. I am getting ids like this:
function uncheckAll(ID) {
var search_form = document.getElementById('frmAccountEdit');
var IsTrialAccount = document.getElementById('IsTrialAccount'); // getElementsByTagName("input");
var IsCustomer = document.getElementById('IsCustomer');
How to make this worked ?
Alright try the below. It will clear both IsTrialAccount and IsCustomer but then check whichever checkbox was checked. If you don't want "IsTrialAccount" to have the same functionality then just don't add it to its button click event and remove the "|| inputs[j].name == "IsCustomer"" part
function uncheckAll(Checkbox)
{
var inputs = document.getElementsByTagName("input");
for(var j = 0; j < inputs.length; j++) {
if(inputs[j].type == "checkbox" &&
(inputs[j].name == "IsTrialAccount" || inputs[j].name == "IsCustomer"))
{
inputs[j].checked = false;
}
}
// Check the selected checkbox
Checkbox.checked = true;
}

Selecting ONE radio out of many

I'm trying to "modify/change" the way this code works, I want it to only allow ONE radio button selection out of 30 or more choices. as it is written now, it looks for all selected before submitting. im a noob, please be kind.
<script type="text/javascript">
function checkform() {
//make sure all picks have a checked value
var f = document.entryForm;
var allChecked = true;
var allR = document.getElementsByTagName('input');
for (var i=0; i < allR.length; i++) {
if(allR[i].type == 'radio') {
if (!radioIsChecked(allR[i].name)) {
allChecked = false;
}
}
}
if (!allChecked) {
return confirm('One or more picks are missing for the current week. Do you wish to submit anyway?');
}
return true;
}
function radioIsChecked(elmName) {
var elements = document.getElementsByName(elmName);
for (var i = 0; i < elements.length; i++) {
if (elements[i].checked) {
return true;
}
}
return false;
}
</script>
Use a counter instead of a flag for "allChecked".
Set it to zero. If the element is checked, use allChecked++ then check to see if the value is ONE at the end.

set first check box checked and others disable from checkbox list in clientside

I have a Checkbox list. On the load of a page i want my first checkbox true and others are disable.I need to check only one checkbox from checkbox list and others should be disable.If use unchecked the checked checkbox then others should be enable means allowed only one check box checked.
My javascript code is here but on the load of page its not checked the first checkbox, also i want the id of each checkbox while using checkboxlist.
function CheckOptions(CheckBoxList) {
var checkboxlist = document.getElementById('CheckBoxList1');
var checkedCount = 0;
var options = CheckBoxList.getElementsByTagName('input');
for (var i = 0; i < options.length; i++) {
if (options[i].checked) {
checkedCount += 1;
}
}
if (checkedCount > 0) {
for (var j = 0; j < options.length; j++) {
if (!options[j].checked)
options[j].disabled = true;
} }
else {
for (var k = 0; k < options.length; k++) {
options[k].disabled = false;
}
}
}
If you're only wanting one checked at a time, it sounds like a group of radio buttons would better serve your purposes.
$(function() {
var checkboxes = $("input[type=checkbox]");
// select the first one on load
checkboxes.eq(0).attr("checked", "checked");
checkboxes.each(function(i, e) {
if (i > 0) {
$(e).attr("disabled", "disabled");
}
})
// handle further selections:
checkboxes.click(function() {
if ($(this).attr("checked")) {
var t = this;
checkboxes.each(function(i, e) {
if (e != t) {
$(e).attr("disabled", "disabled");
}
});
} else {
checkboxes.attr("disabled", null)
}
});
});

how to disable all listbox in a form

how to disable all listbox in a form
Just to improve on Gavin's correct answer:
var selects = theform.getElementsByTagName("select");
for (i = 0; i < selects.length; i++) {
selects[i].disabled = true;
}
$('#formId select').attr('disabled', 'disabled');
Edit: oops, thought I saw a jquery tag. Anyway:-
for (i = 0; i < theform.length; i++) {
var formElement = theform.elements[i];
if (formElement.tagName === "SELECT") {
formElement.disabled = true;
}
}

Categories