3

I'm trying to prevent submiting using an Onclick event (with javascript) if any of selected menus are empty. Maybe displaying an alert. Is it possible?

<select name="form1" form="my_form">
    <option value="Option 1">Option 1</option>
    <option value="Option 2">Option 2</option>
</select>

<select name="form2" form="my_form">
    <option value="Option 1">Option 1</option>
    <option value="Option 2">Option 2</option>
</select>

<form id="my_form" method="post" action="selections.php">
    <input type="submit" value="Send">
</form>
ButchMonkey
  • 1,873
  • 18
  • 30
Alan Fabricio
  • 35
  • 1
  • 8

5 Answers5

5

Proposed solution

The most convenient way is to use HTML form validations handled by a browser (required) as mentioned by epascarello

Unfortunately, your selects has the first option as the default. You can just add a placeholder option with no value. That will prevent submitting the form without selecting.

If you don't want to display it you can hide this option for the user. Second select in the example.

Third input added to answer your question in the comment. No value means it will be content of <option> tag be the default. It would only work if you have no text in this option.

<form id="my_form">
    <select required name="form1" form="my_form">
        <option value>Placeholder</option>
        <option value="Option 1">Option 1</option>
        <option value="Option 2">Option 2</option>
    </select>

    <select required name="form2" form="my_form">
        <option hidden disabled selected value>Choose an option</option>
        <option value="Option 1">Option 1</option>
        <option value="Option 2">Option 2</option>
    </select>

    <select required name="form3" form="my_form">
        <option>I'm the value if none set</option>
        <option value="Option 1">Option 1</option>
        <option value="Option 2">Option 2</option>
    </select>


    <input type="submit" value="Send">
</form>
stefanowiczp
  • 475
  • 5
  • 13
0

I would use an id for your submit button:

<input type="submit" value="Send" id="submitButton" disabled>

Afterwards just create a function for your options:

    function onItemSelected() {
       var selectedItem1 = document.getElementByName("form1").selectedIndex;
       var selectedItem2 = document.getElementByName("form2").selectedIndex;
       if (selectedItem1 && selectedItem2) {
         document.getElementById('submitButton').disabled = false;
       } else {
             document.getElementById('submitButton').disabled = true;
          }
        }

You have to call the onItemSelected in each select input: For example:

<select name="form2" form="my_form" (onchange)="onItemSelected()">
StyrianDev
  • 254
  • 1
  • 8
  • I made work like this but had to do some different stuff. First I took off the parenthesis from "onchange" to call the event properly. Afterwards I used getElementById within the function instead of getElementByName and added de corresponding ID's inside the – Alan Fabricio Feb 06 '20 at 15:17
0

Yes, you can prevent submitting the form if menu are empty using required property in in select tag.

Ashish
  • 410
  • 2
  • 11
0

is it normal that your select inputs are not in your 'form' tag? in which case would they be empty? because 'option 1' is selected by default

  • Yes. The reason for that is that I need to pass values from multiple select forms. To make that possible, I'm using the attribute form="my_form" inside – Alan Fabricio Feb 06 '20 at 14:55
0
var selectElts = document.getElementsByTagName('select');
var formElt = document.getElementById('my_form');

formElt.addEventListener('submit', function(e) {
  // stop the form submission
  e.preventDefault();
  var error = 0;

  // Check all select menus
  for (var i = 0; i < selectElts.length; i++) {
    // Check if current input's value is empty
    if (selectElts[i].value.trim() === '') {
      error = error + 1;
      alert(selectElts[i].name + ' is empty');
    }
  }

  // if there are no errors, then we submit the form
  if (error === 0) {
    formElt.submit();
  } 
});