working from this little form validation script, does anyone know how to check to make sure at least one of several looped in dropdowns have been selected as well. The dropdowns have a php generated name:

<select name="participantqty[<?= $c_row ?>]" id="" class="">

<script type="text/javascript">
 function required(){
    var first = document.forms["register1"].elements["fname"].value;
    var last = document.forms["register1"].elements["lname"].value;
    var email = document.forms["register1"].elements["email"].value;
    var message = document.forms["register1"].elements["phone"].value;
 
    if(first == null || first == "" || last == null || last == ""){
        alert("First and last name fields are required.");
        return false;
    }
    else if(email == null || email == ""){
        alert('An email address is required.');
        return false;
    } 
    else if(message == null || message == ""){
        alert('A phone number is required.');
        return false;
    }else{return true;}
}
 </script>

Recommended Answers

All 3 Replies

There is the :
document.getElementsByTagName(tagname)
from:
http://www.w3schools.com/jsref/met_doc_getelementsbytagname.asp

I don't know that mehod, but probably returns all the elements of a specific tag type:
document.getElementsByTagName("select")

You can loop those and check for each one of those their selectedIndex. For the API of the select element refer to the link I provided

Edit:
Also I will ask for your post to be moved to the javascript forum

hah! Yeah! I was wondering where my post was! I didn't realize I'd posted to a different forum.

I'm not sure the tagname method would work, because there are other dropdowns in the form that are unrelated to the one's I need to check. I'm thinking I could apply a class name to them and pull them into an array somehow - then check to make sure at least one is not "0"? Anyone on here done something like that before? I've never really tried working off class names for javascript before.

-oh... maybe if I put them in a unique div. and searched for tagnames only within that div? hmmm. Time to try and figure this one out.

When you get all the select tags with that method, you can get the name of each one of them and check if it starts with 'participantqty'

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.