ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
i use js to validate a html form. all works well except when i try to check if any one option of a multiple select element has been selected (none selected by default). i really don't know how to address the element in js.
the following code works for text boxes, but not for my multiple=multiple element.
Code:
function kontakt_validate() {
if(document.Formular.field.value == "") {
alert("not like this);
document.Formular.field.focus();
return false;
}
thanks for your help.
however, i can't get it to work.
since i use js only for validation, i am always in big trouble if code doesn't work as expected. maybe you can have a 2nd look?
this is the releveant part of the html/php file:
Code:
<select id="wideselec" name="Leistungen[]" size="8" multiple="multiple">
<option value="Beratung zur materiellen Existenzsicherung">Beratung zur materiellen Existenzsicherung</option>
<option value="Beratung bei Schulden">Beratung bei Schulden</option>
it's necessary in my case to append [] at the end of the name, to be able to handle the array in php. could that be a problem?
this now is how i used your example:
Code:
function kontakt_validate(myform) {
var sel = myform.Leistungen;
var num = 0;
alert("hallo");
for ( var i=0 ; i<sel.options.length ; i++ )
if (sel.options[i].selected)
num++;
if (!num) {
alert("Select some numbers please");
sel.focus();
return false;
}
else {
alert("boo");
}
return true;
}
it doesn't pop up, also setting sel to "document.Formular.Leistungen[]" doesn't help... the code isn't even parsed after that line if i put it like that. (i know that from putting in those alert boxes... sadly enough the only means of "debugging" i know in js) so i assume the square brackets don't belong here in js.
neither one of the alert boxes in "if" or "else" have ever been executed.
ok, it's definetely the name of the multiple field that causes me trouble. if i print the value of the variable "sel" in an alert box i get an "undefined". when trying that with a normal select box i get [object HTMLSelectElement]...
so i guess the square brackets are my problem...
I think this will:
var sel = document.getElementById('wideselec');
it does!! thank you very much, Guttorm!
i'd like to ask one last question, if you don't mind: can you recommend a good online resource for learning javascript. i know my way around bash, php, html and css quite well, have also peeked into python and had to study c++ at one point. except for c++, i found that for those languages there is better material available for study online than in books. is it like that with js, too?
As for studying Javascript, I read the book "Javascript - The definite Guide" by O'Reilly, and I think it's a very good book. It's a bit old now, but I haven't seen anything better. I've seen lots of references online, but no real good tutorial. Maybe someone else knows?
For a language reference site, I like www.gotapi.com. It covers a lot of different languages, including javascript.
yes, language reference is available online, but no good tutorial indeed. thanks for the book recommendation, however... i'll get that one then. (matter of fact, i had it in my hands yesterday when i was in the book store... i was discouraged by its enourmous size, must be 1000 pages or something. well ok, if that is the K&R for javascript, so be it! maybe i'll start using js for more than just validation in the future.)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.