File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes HTML, CSS and JavaScript and the fly likes How to Validate more then one selection list? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "How to Validate more then one selection list?" Watch "How to Validate more then one selection list?" New topic

How to Validate more then one selection list?

Steve Sol

Joined: Apr 18, 2009
Posts: 4
Please could someone help me. I want to be able to make a user fill in both of these selection lists, and if not .. the user would be prompted to, before the form can be sent.
Im only able to validate the first list, unsure on how to add the second list in.
Here is my code.
<script language="JavaScript">

function checkList(selection) {

if (selection.length == 0) {
window.alert("Make a selection.");
return false;

return true;


function checkForm(formObj) {
return checkList(formObj.myField.value);

// -->



<form name="myForm" action="java.html" onSubmit="return checkForm(this);">

Choose 1
<select name="myField">
<option value=""></option>
<option value="1">1</option>

Choose 2
<select name="myField2">
<option value=""></option>
<option value="2">1</option>

<input type="submit">



Eric Pascarello

Joined: Nov 08, 2001
Posts: 15385
When you post code please use code tags so it is easier to read.

You need to use the and [&&] operator

Austin Tashis

Joined: May 23, 2003
Posts: 19
The quickest way to validate a form is to iterate the elements collection and use a switch statement to choose validation criteria by name, id, class, tagName, type or whatever, depending on how specific your validation needs to get.
I agree. Here's the link:
subject: How to Validate more then one selection list?
It's not a secret anymore!