8 Replies - 1405 Views - Last Post: 05 May 2010 - 02:25 PM

#1 ebadamageplan  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 111
  • Joined: 09-February 09

Undefined

Posted 01 May 2010 - 12:39 AM

So I'm writing a quick validation script and I can't figure out why this code isn't working.
It tells my my array is undefined.
I'll comment in the code.

function err(){
	alert("Please enter the highlighted information");
}

function validText(x){
	if(x.value == ""){ 
		err();
		x.focus();
	}
}

function validForm(form) {
	for(var i = 0; i < form.elements.length; i++) {
		if(form.elements[i].value == "") {
			err();
			form.elements[i].focus()
			return false;
		}
	}
	for(var i = 9; i < 14; i++) {
		if(form.elements[i].value == 0) {
			alert("Please select an option.");
			form.elements[i].focus()
			return false;
		}
	}
	var radio = form.elements["search"];           // search is the name of all the radio buttons
	for(var i = 0; i < radio.length+1; i++) { 
		if(radio[i].checked) return true;      // Here's the error "radio[i] is undefined"
		if(i == radio.length+1) return false;
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Undefined

#2 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1385
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Undefined

Posted 01 May 2010 - 01:37 AM

From a quick look, your problem appears to be here:
for(var i = 0; i < radio.length+1; i++)

Why do you have +1?

Your undefined sounds like it's coming right at the end, when you've looped too far. :)

This post has been edited by gabehabe: 01 May 2010 - 01:38 AM

Was This Post Helpful? 1
  • +
  • -

#3 ebadamageplan  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 111
  • Joined: 09-February 09

Re: Undefined

Posted 01 May 2010 - 10:22 AM

Okay I took that out, my logic there was flawed. But now I'm getting an error and I don't know what it means.

Error: uncaught exception: [Exception... "Cannot modify properties of a WrappedNative"  nsresult: "0x80570034 (NS_ERROR_XPC_CANT_MODIFY_PROP_ON_WN)"  location: "JS frame :: chrome://global/content/bindings/autocomplete.xml :: onxblpopuphiding :: line 825"  data: no]



This happens if a radio button is chosen or not so either way my test for the radio buttons still isn't working.

Also my name's Gabe
Was This Post Helpful? 0
  • +
  • -

#4 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1385
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Undefined

Posted 02 May 2010 - 04:12 AM

My name's Danny, gabehabe is just a screen name... long story. ;)

As for you error, I honestly don't know. I've done a bit of digging, and chrome://global/content/bindings/autocomplete.xml is a firefox resource file. I opened mine, but after some digging, I found that there's apparently loads of different versions, so mine could be different to yours.

Can you grab the code out of chrome://global/content/bindings/autocomplete.xml and paste it here for me?

Not sure how helpful I can be here, but worth a shot. :)

This post has been edited by gabehabe: 02 May 2010 - 04:12 AM

Was This Post Helpful? 1
  • +
  • -

#5 ebadamageplan  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 111
  • Joined: 09-February 09

Re: Undefined

Posted 02 May 2010 - 09:22 AM

I'm not too worried about that weird chrome:// error because it seems to be pretty random and it doesn't break anything. Maybe I'll worry about it when the script works.

Anyway, here's the code after the change you suggested and a few others.
Now I only get the undefined error if no radio button is selected, which would be fine if that did me any good. Is there a way I can use that to test?

function err(){
	alert("Please enter the highlighted information");
}

function validText(x){
	if(x.value == ""){ 
		err();
		x.focus();
		return false;
	}
}

function validForm(form) {
	for(var i = 0; i < 9; i++) {
		if(form.elements[i].value == "") {
			err();
			form.elements[i].focus()
			return false;
		}
	}
	for(var i = 9; i < 11; i++) {
		if(form.elements[i].value == 0) {
			alert("Please select an option.");
			form.elements[i].focus()
			return false;
		}
	}
	var radio = form.elements["search"];
	for(var j = 0; j <= radio.length; j++) { 
		if(radio[j].checked) return true;
		if(j == radio.length) return false;
	}
}


Was This Post Helpful? 0
  • +
  • -

#6 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1385
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Undefined

Posted 03 May 2010 - 06:54 AM

for(var j = 0; j <= radio.length; j++)
for(var j = 0; j < radio.length; j++)
Spot the difference. ;)

It's giving you an undefined because it's still looping that extra one time.

Think:
If you have 3 radio buttons, radio.length will be 3.
You're looping from 0.
<= will check for: 0, 1, 2, 3
< will check for: 0, 1, 2

:)

This post has been edited by gabehabe: 03 May 2010 - 06:58 AM

Was This Post Helpful? 1
  • +
  • -

#7 ebadamageplan  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 111
  • Joined: 09-February 09

Re: Undefined

Posted 03 May 2010 - 09:27 AM

Ugh it's always something simple. That fixed the undefined but it still isn't returning anything if no radio button is selected.

var radio = form.elements["search"];
	for(i = 0; i < radio.length; i++) { 
		if(radio[i].checked) return true;
		if(i == radio.length) {
			alert("Please choose a search option");
			return false;
		}
	}


Was This Post Helpful? 0
  • +
  • -

#8 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1385
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Undefined

Posted 03 May 2010 - 09:30 AM

i will never reach radio.length :)

if(i == radio.length) // ...

Instead, try something like this:
var radio = form.elements["search"];
for(i = 0; i < radio.length; i++) { 
    if(radio[i].checked) return true;
}
// reached the end without finding any that have been checked
alert("Please choose a search option");
return false;



Alternatively, changing if(i == radio.length) to if(i == radio.length-1) could work.
Was This Post Helpful? 1
  • +
  • -

#9 ebadamageplan  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 111
  • Joined: 09-February 09

Re: Undefined

Posted 05 May 2010 - 02:25 PM

Awesome man, that did the trick, thanks a lot.

I'm having some PHP issue in this thread if you know anything about that and have time to help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1