Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Apply the HTML5 required
attribute on any form field that must be completed in order to submit
the form:
<form>
<fieldset>
<legend>Login</legend>
<p><label>Username <input type="text" name="username" required></label></p>
<p><label>Password <input type="password" name="pwd" required></label></p>
</fieldset>
<p><button type="submit">Submit</button></p>
</form>
The required attribute may be
specified on zero or more form elements in a form.
When the user submits the form, any required field that has been left blank will cause the browser to stop the submission and an error message will be displayed. The visual display of the error varies from browser to browser, as shown in Figures 3-21–3-24.