user1794625
user1794625

Reputation: 105

Javascript dropdown validation and alert

I have tried to get it to work but I simply can't find the mistake. Also I have no idea how to make the selection which you have not selected to turn red or some color to alert you.

<!DOCTYPE html>
<html>
    <body>
        <select id='Selection' name='Selection'>
            <option value=''>Select</option>
            <option value='1'>user1</option>
            <option value='2'>user2</option>
            <option value='3'>user3</option>
        </select>
        <br>
        <select id='Candidate' name='Candidate'>
            <option value=''>Select</option>
            <option value='1'>candidate1</option>
            <option value='2'>candidate2</option>
            <option value='3'>candidate3</option>
        </select>  
        <br>  
        <input type='button' onclick='Validate()' value='select' />
        <script>
            function Validate()
            {
              if(document.getElementById('Selection').value == '' ||
                 document.getElementById('Candidate').value == '' ||)                
              {
                alert('Please complete all selections');
                return false;
              }
              return true;
            }       
        </script>
    </body>
</html>

Upvotes: 0

Views: 1687

Answers (1)

Musa
Musa

Reputation: 97717

You have an extra || in you if condition, this will cause a syntax error, remove it (the last one not both). To change the element that has an invalid value you can just use css but you'll have to check them individually.

Upvotes: 1

Related Questions