Jump to content
Larry Ullman's Book Forums

Ch5, P146, The Required Attribute Of The Select Element


Recommended Posts

    <form action="" method="post" id="theForm">
        <fieldset><legend>Create Your Membership</legend>
            <p>Complete this form to calculate your membership. There's a 20% discount if you enroll for more than one year!</p>
			<div><label for="type">Type</label><select name="type" id="type" required>
			    <option value="basic">Basic - $10.00</option>
			    <option value="premium">Premium - $15.00</option>
			    <option value="gold">Gold - $20.00</option>
			    <option value="platinum">Platinum - $25.00</option>
			</select></div>
			<div><label for="years">Years</label><input type="number" name="years" id="years" min="1" required></div>
			<div><label for="cost">Cost</label><input type="text" name="cost" id="cost" disabled></div>
			<input type="submit" value="Calculate" id="submit">
        </fieldset>
    </form>

I think the required attribute in this select element may not be correct. Netbeans indicates this is an error, and I checked online:

 

http://dev.w3.org/html5/spec-author-view/the-select-element.html#the-select-element

When there is no default option, a placeholder can be used instead:

<select name="unittype" required>
 <option value=""> Select unit type </option>
 <option value="1"> Miner </option>
 <option value="2"> Puffer </option>
 <option value="3"> Snipey </option>
 <option value="4"> Max </option>
 <option value="5"> Firebot </option>
</select>

seems if required is used, then the first option's value should be empty.

Link to comment
Share on other sites

The required attribute can be used for input elements and select elements only:

http://www.w3schools.com/tags/att_input_required.asp

 

Also, just writing "required" is valid, as explained in the following:

http://stackoverflow.com/questions/3004703/required-attribute-html5

 

Lastly, you can choose whether or not to have a default value for a select element that uses required.

There's no requirement to do one versus the other.

Link to comment
Share on other sites

I wouldn't rely too much upon what Netbeans indicates is an error when it comes to HTML. Also remember that there's often a huge difference between what is in the HTML specs and what browsers actually do or are capable of handling.

Link to comment
Share on other sites

 Share

×
×
  • Create New...