Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jan 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation How to alert members when there is no values selected from a multiple select form

    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!!!

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,734
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    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!");
    }
    }

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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.
    Last edited by Kor; 01-20-2006 at 11:14 AM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,734
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by Kor
    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!
    Last edited by Philip M; 01-20-2006 at 07:19 PM.

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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.
    Quote Originally Posted by Philip M
    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
    Last edited by Kor; 01-21-2006 at 02:50 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •