3
<form action="#" class="group" method="post" onsubmit="return checkforblank()">
    <legend><span class="number">1</span>Departing & Arriving</legend>
    <fielset class="col-sm-6">
        <label for="FDestination">From</label>
        <select name="Location" id="Location">
            <option value="Please Select">Please Select</option>
            <option value="Newport">Newport</option>
            <option value="Mahdi">Mahdi</option>
            <option value="Cardiff">Cardiff</option>
            <option value="Cilo">Cilo is</option>
        </select>
    </fieldset>
</form>

<script>
    function checkforblank(){
        if (document.getElementsByID('Location').value== "Please Select") {
            alert('Please enter first name');
            document.getElementById('Location').style.borderColor = "red"; 
            return false; 
        }
    }
</script>

How can I display error when user chooses "Please Select" option from fieldset?

3
  • 1
    There is a typo in your post. document.getElementsByID('Location').value should be document.getElementById('Location').value Commented Nov 6, 2015 at 21:40
  • @Payson I'd suggest posting this as the answer. Also, try to take a look into using dev tools in whichever browser you are using to test the code. This would be a great asset in saving time and frustration in the future :) Commented Nov 6, 2015 at 21:45
  • @LukeG thanks. Good tips Commented Nov 6, 2015 at 21:52

4 Answers 4

4

How can I display error when user chooses "Please Select" option from fieldset?

You are already validating form submit, now you only need to add the same validation function for select onchange:

function checkforblank() {
    
    var location = document.getElementById('Location');
    var invalid = location.value == "Please Select";
 
    if (invalid) {
        alert('Please enter first name');
        location.className = 'error';
    }
    else {
        location.className = '';
    }
    
    return !invalid;
}
.error {border: 1px red solid;}
<form action="#" class="group" method="post" onsubmit="return checkforblank()">
    <legend><span class="number">1</span>Departing & Arriving</legend>
    <fielset class="col-sm-6">
        <label for="FDestination">From</label>
        <select name="Location" id="Location" onchange="checkforblank()">
            <option value="Please Select">Please Select</option>
            <option value="Newport">Newport</option>
            <option value="Mahdi">Mahdi</option>
            <option value="Cardiff">Cardiff</option>
            <option value="Cilo">Cilo is</option>
        </select>
    </fieldset>
    <button>Save</button>
</form>

You will not see alert in demo, as it's not allowed in snippet sandboxed iframe :(

Sign up to request clarification or add additional context in comments.

1 Comment

Thankyou very much! , say if I had another select option with exact same options and value also ID's how can i make the data validate there aswell? should I just create a process for that part again or is there a shorter way for me to use exact same validation without having to write another process again.
3

Why not use the required keyword? Requires HTML5, but doesn't need to rely on JS. For additional info, see the documentation.

:required:focus {
  box-shadow: 0  0 6px rgba(255,0,0,0.5);
  border: 1px red solid;
  outline: 0;
}
<form action="#" class="group" method="post" onsubmit="return checkforblank()">
  <legend><span class="number">1</span>Departing & Arriving</legend>
  <fielset class="col-sm-6">
    <label for="FDestination">From</label>
    <select name="Location" id="Location" required>
      <option value="">Please select</option>
      <option value="Newport">Newport</option>
      <option value="Mahdi">Mahdi</option>
      <option value="Cardiff">Cardiff</option>
      <option value="Cilo">Cilo is</option>
    </select>
    </fieldset>
    <input type="submit" value="Submit">
</form>


After noticing that this wasn't working well on all browsers, or on all inputs I made a fiddle with many input types (but not all HTML5 ones are included). This way, you can check if the required attribute is working in the browsers that you need. At this time, a JS might indeed be your best bet to be completely cross-browser safe.

1 Comment

You can also add disabled and hidden keywords to make the "Please select" option unclickable. <option selected disabled hidden value="">Please select</option>
1

function validateForm() {    
  if (document.getElementById('Location').value== "Please Select")
  {
      alert('Please enter first name');
      document.getElementById('Location').style.borderColor = "red"; 
      return false; 
  }    
}
<form name="myForm" action="#"
onsubmit="return validateForm()" method="post">
<select name="Location" id = "Location" >
  <option value = "Please Select" >Please Select </option>
  <option value="Newport">Newport</option>
  <option value="Mahdi">Mahdi</option>
  <option value="Cardiff">Cardiff</option>
  <option value="Cilo">Cilo is </option>
</select>
<input type="submit" value="Click">
</form>

Comments

0

This is a pure HTML solution:

make your select tag required, make your first option selected, disabled, and add the value attribute and leave it empty: (value=""), if they don't choose anything, it throws a frontend validation error asking them to change the value since it is empty.

        <select required name="Location" id="Location">
            <option disabled selected value="">Please Select</option>
            <option value="Newport">Newport</option>
            <option value="Mahdi">Mahdi</option>
            <option value="Cardiff">Cardiff</option>
            <option value="Cilo">Cilo is</option>
        </select>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.