Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate HTML select tag without javascript

Tags:

html

forms

Our select tags display an empty option tag at the beginning to ensure users don't automatically select the first option otherwise displayed:

<select name="title" required pattern="[A-Z]">
<option value="0"></option>
<option value="Mr">Mr</option>
<option value="Mrs">Mrs</option>
<option value="Ms">Ms</option>
</select>

How do we validate the select tag to ensure one of the options OTHER than 0 is selected?

Perferably without Javascript!!

like image 638
Richard Tinkler Avatar asked Feb 17 '15 15:02

Richard Tinkler


1 Answers

Set value="" for the first option and the required validation will behave as expected:

<form>
  <select name="title" required>
    <option value="">Select One</option>
    <option value="1">First Value</option>
  </select>
  <button>submit</button>
</form>

Keep in mind browser support when leveraging HTML5 form validation.

like image 157
canon Avatar answered Oct 17 '22 00:10

canon