Dan
Dan

Reputation: 13

JavaScript - dropdown validation selection

I have 3 dropdowns, I would like to check in JavaScript that at least one selection has been made, if not display a message. What would be the simplest way to do it in JavaScript?

id: dropdown1 id: dropdown2 id: dropdown3

Thanks

Upvotes: 1

Views: 5923

Answers (1)

Gerrat
Gerrat

Reputation: 29690

// need to make sure the first dropdown option is blank, then...

var dd1 = document.getElementById('dropdown1');
var dd2 = document.getElementById('dropdown2');
var dd3 = document.getElementById('dropdown3');

if (
  (dd1.selectedIndex == 0) && 
  (dd2.selectedIndex == 0) && 
  (dd3.selectedIndex == 0)
) {  
  alert('need to select at least one')
}

Upvotes: 1

Related Questions