...

View Full Version : How to alert members when there is no values selected from a multiple select form



newbieBells
01-20-2006, 06:57 AM
This is my problem: State is mandatory in my form and the javascript alerts members if they have not selected a state. Now I changed my state select field to a multiple selection which made an error in javascript already. The state has become an array already and my simple script does not work anymore.

My previous code:
if (document.PostForm.state.value == "")
{
window.alert("State is mandatory. Please select a state.");
document.Postform.state.focus();
return false;
}

I have tried create a new function that would get the values from my multiple select field and if ever there are no values selected, the script should prompt the user that state is mandatory and they should select at least one. I've tried but none are correct.. Hehe.. Could anyone please help me.. Just a newbie in javascript...

This is the code that my friend and I did.
function checkStates() {
var NumState = 0;
document.PostForm.hid_state.value = "";
for(var i=0; i< document.PostForm.state.options.length; i++) {
if(document.PostForm.state.options[i].selected ) {
NumState = NumState + 1;
document.PostForm.hid_state.value = document.PostForm.hid_state.value + "|" + document.PostForm.state.options[i].value
}
}
if ( NumState == 0 ) {
window.alert('Enter your state, please!');
document.PostForm.state.focus();
return false;
}
else {
return true;
}

Please help! Thanks!!!

Philip M
01-20-2006, 07:57 AM
An options[] array has a property called selectedIndex that contains the index number of the item currently selected.

function show(){
var picked = document.form.selectName.selectedIndex;
alert (picked);
if (picked <=0) {
alert ("You must select an option!");
}
}

Kor
01-20-2006, 11:10 AM
An options[] array

It's not quite an array, but a collection of objects, which is not exactly the same thing. But for the fast understanding of that problem/solution, the solving is correct.

Philip M
01-20-2006, 07:17 PM
It's not quite an array, but a collection of objects, which is not exactly the same thing. But for the fast understanding of that problem/solution, the solving is correct.

I bow to your superior knowledge! And I am sure that you are correct. But my (possibly rather basic) textbook calls it an array.

By the way, why on earth would the Romanians wish to join the EU? Surely they have had enough of rabid socialism, mad bureaucracy, unaccountable and unelected politicians, endless state interference, thought police etc.? Many people now in the EU against their will would be happy to get out!

Kor
01-21-2006, 09:40 AM
It is only a matter of methods which might be applied uppon arrays and objects' collections. Objects' collections are arrays by structure, but not entirely by behaviour. There are certain arrays' methods (such as concat(), for instance) which are not supported by the objects' collections.


By the way, why on earth would the Romanians wish to join the EU?

Would have studied our history, you could see that Romanians first worry all the times was to defend against the Eastern invadors, Russians mainly. We have no other option than to join a strong economical and military alliance to avoid for the future a nightmare such the Communism brought by the Russians after the WW2. This is the reason for we joined NATO and we try to join EU. We are the only Latin people whithin this part of Europe, surrounded mainly by Slavs. It was not easy for us to stand firm along the history. We do belong to the European culture, not to the Asian culture, thus our options were always turned to the West.

During WWI we entered the war to support France and Britain, even we haven't been attacked by the Germans. During WWII we had to make a painful choise between Allies and Axis. We chosed to fight against the Soviet Union, even we did not like Hitler, because the Russians have had previousely ocupied 1/5 of our teritory (the present Republic of Moldavia). We wanted to fight only against USSR, the same way Finland did at the moment. By the way in 1941 we have declared war only to the USSR. We have never felt that we were in war against UK or USA, even you have bombed now and then some of our cities.

As about enter/exit EU... Well, maybe Brittany can do it by her own. We can not survive alone. This is it...
-------
By the way, I am a steady and old supporter of a British footbal team, Manchester United. Well, if you live in London you might not like that :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum