6

Possible Duplicate:
Can I apply the required attribute to <select> fields in HTML5?

I'm using the html5 contact form from here but I'm having issues trying to make the <select> field required. Everything else works fine, just having trouble with this and the dev hasnt responded when I asked about the field. this is what I have

 <select name="package" id="package" title="Please choose a package" class="required">
    <option value="">------------------</option>
    <option value="basic">Basic</option>
    <option value="plus">Plus</option>
    <option value="premium">Premium</option>
</select>

What am I missing to get this working correctly?

4
  • It's easy with javascript Commented Jan 16, 2013 at 7:40
  • What browser are you using?
    – Luka
    Commented Jan 16, 2013 at 7:40
  • Can you provide CSS, interested to see what is in "required" style. Also, are you using any javascript validation, if not you can use simple javascript validation - if(package.selectedIndex<=0) //value not selected
    – Umesh
    Commented Jan 16, 2013 at 7:45
  • stackoverflow.com/questions/8287353/…
    – Anshu
    Commented Jan 16, 2013 at 7:46

3 Answers 3

17

I believe that the top answer to this question is what you're looking for. To quote,

This works for me - Have the first value empty - required works on empty values.

<select required> <option value="">Please select</option> <option value="one">One</option> </select>

0
0

check this: http://www.w3.org/TR/html-markup/select.html

global attributes Any attributes permitted globally.

  1. name = string

    The name part of the name/value pair associated with this element for the purposes of form submission.

  2. disabled = "disabled" or "" (empty string) or empty

    Specifies that the element represents a disabled control.

  3. form = ID reference NEW

    The value of the id attribute on the form with which to associate the element.

  4. size = positive integer

    The number of options to show to the user.

  5. multiple = "multiple" or "" (empty string) or empty

    If present, indicates that its select element represents a control for selecting zero or more options from a list of options. If not present, indicates that its select element represents a control for selecting a single option from a list of options.

  6. autofocus = "autofocus" or "" (empty string) or empty

    Specifies that the element represents a control to which a UA is meant to give focus as soon as the document is loaded.

  7. required = "required" or "" (empty string) or empty

    Specifies that the element is a required part of form submission.

so you require a required = "required" attribute to your <select>

0

If you have a better browser, you can try this (like in this thread):

<select required>
  <option value=""></option>
  <option value="basic">Basic</option>
  <option value="plus">Plus</option>
  <option value="premium">Premium</option>
</select>

, but still you should use JavaScript because not a lot of people have browsers that support HTML5 required attribute. It's not supported by IE and Safari.

1
  • If You will put aria-required="true" it will work on Safari, Beside webside have to check on server side if that field has been set so it's just a mater of taste to check it in JS. Personally i think that users using ie should blame themselves :)
    – bumerang
    Commented Jan 16, 2013 at 7:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.