3

I have the following code on a required select :

<select required="required">
    <option value="1">01</option>
    <option value="2">02</option>
    ...
    <option value="31">31</option>
</select>

In my (HTML5 / symfony2) app its a required date (day) so the value must be between 1 and 31 and I do not want blank/null values.

Even though the code works, it breaks w3c validation with the following error :

The first child option element of a select element with a required attribute and without a multiple attribute, and whose size is 1, must have either an empty value attribute, or must have no text content.

What is the best approach to the problem

1 Answer 1

8

With require attribute:

<select required="required">
<option value=""></option><!-- added empty value -->
<option value="1">01</option>
<option value="2">02</option>
...
<option value="31">31</option>
</select>

Without – just remove require attribute:

<select>
<option value="1">01</option>
<option value="2">02</option>
...
<option value="31">31</option>
</select>
Sign up to request clarification or add additional context in comments.

3 Comments

If you don't want to include the blank option another way you can do this is by setting the selectedIndex of the <select> to -1.
Right, but I don't know if it pass the w3c validator. It's JS solution and in fact empty option value (mentioned by validator) will still be missing. Needs test.
It'll pass the validator, but I agree that the empty <option> is the better solution. I was just throwing it out there that it will work.

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.