Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

3. Forms > 3.8. Requiring a Form Field

3.8. Requiring a Form Field

Problem

You want to require a form field’s completion prior to form submission.

Solution

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>

Discussion

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-213-24.


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial