|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Error with passing an undefined array to a function
Hi all!
I need a function that validates inputs onSubmit. I have a page that submits to itself a number of times, and input validation is different between the 1st time the page is submitted, and all the subsequent times. The validation involves making sure that one of 2 groups of checkboxes has at least one of the boxes checked. Basically, on first submission, I need at least one box checked in group 1, but on each subsequent submission, I need at least one box checked from either group. This sounds simple, but here is the complication: After the 1st submission, the boxes from group 1 may not appear at all, and the function would be counting a non-existent array. This is where it stop executing and just submits the page. I tried different ways to prevent it from counting the non-existent array, but nothing I did helped. I tried all these, if not more: Code:
if (document[whichForm][array1] instanceof Array) if (typeof(document[whichForm][array1][i]) != 'undefined') if (typeof(document[whichForm][array1][0]) == 'undefined') document[whichForm][array1][0] = 0; if (document[whichForm][array1][i].type =="checkbox") Here is my function. The trouble is when array1's values are undefined. Code:
function howManyChecked(whichForm, array1, array2, myMin) {
var countCheckedSO = 0;
var countCheckedAO = 0;
var err = 0;
for (var i=0; i<document[whichForm][array1].length; i++) {
if (document[whichForm][array1][i].checked==true) {
countCheckedSO++;
}
}
//if (document[whichForm][array2] instanceof Array)
for (i=0; i<document[whichForm][array2].length; i++) {
if (document[whichForm][array2][i].checked==true) {
countCheckedAO++;
}
}
//alert ('so='+countCheckedSO+', ao='+countCheckedAO);
if (countCheckedSO < myMin && <?=$_POST['this_col']?> == 1) {
alert('You must select at least '+myMin+' entry for Services');
err = 1;
}
else if (countCheckedSO < myMin && countCheckedAO < myMin) {
alert('You must select at least '+myMin+' entry');
err = 1;
}
if (err == 1) { return false; }
}
|
|
#2
|
||||
|
||||
|
Try the first for loop like this. It probably would be sufficient to test just the first element for undefined:
Code:
if (array1 && array1.length) for (var i=0; i<array1.length; i++) if (array1[i]!=undefined && array1[i].checked) countCheckedSO++; |
|
#3
|
|||
|
|||
|
Quote:
Thanks. But it still doesn't work. It never increments "countCheckedSO", even though the checkboxes are checked. I also tried "if (array1[i]!='undefined' && array1[i].checked)" (with the quotes), and the result is still the same. ![]() |
|
#4
|
||||
|
||||
|
yigalm, how are you calling the function anyway?
__________________
Spreading knowledge, one newbie at a time. Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions IE7: the generation 7 browser new in a world of generation 8 browsers. Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around. |
|
#5
|
|||
|
|||
|
Quote:
onsubmit="return howManyChecked('pform', 'sub_opco[]', 'add_on[]', 1); The function does work when sub_opcos[] (array1) has values. Thanks. |
|
#6
|
||||
|
||||
|
Try this.
Code:
function howManyChecked(whichForm, array1, array2, myMin) {
var countCheckedSO = 0;
var countCheckedAO = 0;
var err = 0;
var f = document[whichForm];
if(!f) return;
if(f[array1] && f[array1].length) {
for (var i=0; i<f[array1].length; i++) {
if (f[array1][i].checked==true) {
countCheckedSO++;
}
}
}
|
|
#7
|
|||
|
|||
|
Quote:
This worked! Thanks so much!!!!! ![]() |
|
#8
|
||||
|
||||
|
You're welcome
![]() By the way, Code:
if (f[array1][i].checked==true) can be shortened to Code:
if (f[array1][i].checked) |
![]() |
| Viewing: Dev Shed Forums > Web Design > JavaScript Development > Error with passing an undefined array to a function |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|