JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 5th, 2008, 03:23 PM
yigalm yigalm is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2001
Posts: 121 yigalm Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 1 Day 1 h 39 m 15 sec
Reputation Power: 0
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; }
}

Reply With Quote
  #2  
Old May 5th, 2008, 08:55 PM
Arty Effem's Avatar
Arty Effem Arty Effem is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2006
Location: England
Posts: 214 Arty Effem User rank is Sergeant Major (2000 - 5000 Reputation Level)Arty Effem User rank is Sergeant Major (2000 - 5000 Reputation Level)Arty Effem User rank is Sergeant Major (2000 - 5000 Reputation Level)Arty Effem User rank is Sergeant Major (2000 - 5000 Reputation Level)Arty Effem User rank is Sergeant Major (2000 - 5000 Reputation Level)Arty Effem User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 4 Days 14 h 44 m 36 sec
Reputation Power: 30
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++;

Reply With Quote
  #3  
Old May 6th, 2008, 12:37 PM
yigalm yigalm is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2001
Posts: 121 yigalm Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 1 Day 1 h 39 m 15 sec
Reputation Power: 0
Quote:
Originally Posted by Arty Effem
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++;

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.

Reply With Quote
  #4  
Old May 6th, 2008, 12:47 PM
Kravvitz's Avatar
Kravvitz Kravvitz is offline
CSS & JS/DOM Adept
Click here for more information.
 
Join Date: Jul 2004
Location: USA
Posts: 15,149 Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 2 Weeks 2 Days 2 h 47 m 18 sec
Reputation Power: 1294
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.

Reply With Quote
  #5  
Old May 6th, 2008, 12:52 PM
yigalm yigalm is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2001
Posts: 121 yigalm Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 1 Day 1 h 39 m 15 sec
Reputation Power: 0
Quote:
Originally Posted by Kravvitz
yigalm, how are you calling the function anyway?

onsubmit="return howManyChecked('pform', 'sub_opco[]', 'add_on[]', 1);

The function does work when sub_opcos[] (array1) has values.

Thanks.

Reply With Quote
  #6  
Old May 6th, 2008, 12:58 PM
Kravvitz's Avatar
Kravvitz Kravvitz is offline
CSS & JS/DOM Adept
Click here for more information.
 
Join Date: Jul 2004
Location: USA
Posts: 15,149 Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 2 Weeks 2 Days 2 h 47 m 18 sec
Reputation Power: 1294
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++;
			}
		}
	}

Reply With Quote
  #7  
Old May 6th, 2008, 01:15 PM
yigalm yigalm is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2001
Posts: 121 yigalm Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 1 Day 1 h 39 m 15 sec
Reputation Power: 0
Quote:
Originally Posted by Kravvitz
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++;
			}
		}
	}


This worked! Thanks so much!!!!!

Reply With Quote
  #8  
Old May 6th, 2008, 02:36 PM
Kravvitz's Avatar
Kravvitz Kravvitz is offline
CSS & JS/DOM Adept
Click here for more information.
 
Join Date: Jul 2004
Location: USA
Posts: 15,149 Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 2 Weeks 2 Days 2 h 47 m 18 sec
Reputation Power: 1294
You're welcome

By the way,
Code:
if (f[array1][i].checked==true)

can be shortened to
Code:
if (f[array1][i].checked)
Comments on this post
yigalm agrees: Thanks a mil! I wasted so much time on this problem.

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Error with passing an undefined array to a function


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway