Validate select box

245,581

Solution 1

Just add a class of required to the select

<select id="select" class="required">

Solution 2

For starters, you can "disable" the option from being selected accidentally by users:

<option value="" disabled="disabled">Choose an option</option>

Then, inside your JavaScript event (doesn't matter whether it is jQuery or JavaScript), for your form to validate whether it is set, do:

select = document.getElementById('select'); // or in jQuery use: select = this;
if (select.value) {
  // value is set to a valid option, so submit form
  return true;
}
return false;

Or something to that effect.

Solution 3

I don't know how was the plugin the time the question was asked (2009), but I faced the same problem today and solved it this way:

  1. Give your select tag a name attribute. For example in this case

    <select name="myselect">

  2. Instead of working with the attribute value="default" in the tag option, disable the default option as suggested by Jeremy Visser or set value=""

    <option disabled="disabled">Choose...</option>

    or

    <option value="">Choose...</option>

  3. Set the plugin validation rule

    $( "#YOUR_FORM_ID" ).validate({ rules: { myselect: { required: true } } });

    or

    <select name="myselect" class="required">

Obs: redsquare's solution works only if you have just one select in your form. If you want his solution to work with more than one select add a name attribute to your select.

Hope it helps! :)

Solution 4

<select id='bookcategory' class="form-control" required="">
                    <option value="" disabled="disabled">Category</option>
                    <option value="1">LITERATURE & FICTION</option>
                    <option value="2">NON FICTION</option>
                    <option value="3">ACADEMIC</option>
            <option value="4">CHILDREN & TEENS</option>

                </select>

HTML form validation can be performed automatically by the browser. Try the above code:
The rest all will be done automatically, no need to create any js functions just this dropdown and a submit button.

Solution 5

const semesterValue = semester.value.trim();

   if(semesterValue == ""){
      setErrorForDropDown(semester,"Please select your semester");
      }else{
      setSuccessForDropDown(semester);
    }
    
function setErrorForDropDown(input,message){
      const formControl = input.parentElement;
      const small = formControl.querySelector('small');
      small.innerText = message;
      formControl.className = "drop-down error";   
    }
function setSuccessForDropDown(input){
      const formControl = input.parentElement;
      formControl.className = 'drop-down success';
    }    
<div class = "drop-down">
                    <label for="semester">Semester</label>
                    <select name="semester" class= "select" id="select">
                        <option value = "">Select</option>
                        <option value="1st">1st</option>
                        <option value="2nd">2nd</option>
                        <option value="3rd">3rd</option>
                        <option value="4th">4th</option>
                        <option value="5th">5th</option>
                        <option value="6th">6th</option>
                        <option value="7th">7th</option>
                        <option value="8th">8th</option>
                    </select>
                    <small></small>
                </div>

Share:
245,581
ponjoh
Author by

ponjoh

Updated on July 05, 2022

Comments

  • ponjoh
    ponjoh almost 2 years

    I'm using the jQuery plugin Validation to validate a form. I have a select list looking like this:

    <select id="select">
    <option value="">Choose an option</option>
    <option value="option1">Option1</option>
    <option value="option2">Option2</option>
    <option value="option3">Option3</option>
    </select>
    

    Now, I want to make sure that the user selects anything but "Choose an option" (which is the default one). So that it won't validate if you choose the first option. How can this be done?

  • tjjjohnson
    tjjjohnson about 13 years
    works good as long as you set value to empty string <option selected="" value=""> Please Select </option>
  • cregox
    cregox about 13 years
    @tjjjohnson actually, it worked for me using just <select class="required"><option /></select> and nothing else. but problem is it didn't pick my custom message. any ideas, @redsquare?
  • redsquare
    redsquare almost 12 years
    @Liam McCann note that this was answered prior to jquery mobile being born!
  • Lemex
    Lemex almost 12 years
    Sorry, Should i open a new question? Didnt want to get mark duplicate or get moaned it ... What should i do?
  • Jonathan Geisler
    Jonathan Geisler almost 8 years
    You may wish to add the selected attribute to the first option so that the browser starts off with that label to make it more visible to the user.
  • Musaddiq Khan
    Musaddiq Khan almost 7 years
    Can we implement on select element change event?
  • Sfili_81
    Sfili_81 almost 3 years
    Welcome to Stack Overflow. Code without any explanation are rarely helpful. Stack Overflow is about learning, not providing snippets to blindly copy and paste. Please edit your question and explain how it answers the specific question being asked. See How to Answer.