Click to See Complete Forum and Search --> : quick quizz


william
02-19-2004, 12:35 AM
hi there, I'm a newbie and this will be piece of cake for you. why the following code doesn't work?

function checkRegion(){
var error = "";
if(document.registration.region.selectedIndex == "")
error = "Região incompleta.\n";
return error;
}

I've several of these checking functions, but particularly this one causes some problems.
when I press submit button it doesn't do anything.
If I take the first line out, it works, but doesn't clean the error when I call it again.

what's the mistery behind it?

Thanks

Kor
02-19-2004, 01:56 AM
selectedIndex must have integer values from 0 to the options length-1. Further more, that must be a number, not a string.


Now:

If your fist option is a inviting one
<select>
<option selected>Choose a region</option>
<option>Region 1</option>
<option>Region 2</option>
...

Your code must be
if(document.registration.region.selectedIndex ==0 )

Note that, even of there is no selected propriety on tag option, the default selectedIndex still has a value, 0, even if there is no focus on it.

william
02-19-2004, 03:21 AM
hey, i got it.
thanks kor.

now i have another problem. Is it possible to use value instead of index ?
not all the entries in the combobox are really valid. some are only cosmetic to make it easier to read.
and i don't know the indexes of all invalid entries,
coz i'm using php to generate combobox.

Thanks

Pittimann
02-19-2004, 03:56 AM
Hi!

Due to the fact, that we don't know your form, take this as an example:

if (document.registration.region.options[document.registration.region.selectedIndex].value=="which value"){
//your code here...
}

Just replace "which value" in the if statement with the value, you want to deal with...

Cheers - Pit

william
02-19-2004, 05:28 AM
hey Pit,

thanks a lot.
your answer is crystal clear.
you save me a lot of time.
this forum is A+++++.

Pittimann
02-19-2004, 05:32 AM
Hi!

You're welcome!!this forum is A+++++That's why I also like it very much! :D

Cheers - Pit