More HTML Forms, Part 2

More Control Elements and Validation

Course weeks and topics 
1. Intro to HTML 56. Page Layout with CSS
2. First Web Page7. HTML Tables
3. Developing a Web Site8. HTML Forms
4. Design with CSS9. Multimedia
5. Midterm10.Term Project

Contents

More HTML Forms, Part 2ContentsIntroductionMore Control ElementsSelection ListsSelection of multiple itemsOption groups for selection lists Option Buttons Access KeysInput ValidationValidation using HTML5 input typesValidation using the required attributeValidation using regular expressionsRegex references

Introduction

 

More Control Elements

Selection Lists

Selection lists present the user with a set of options from which they may select one (or more?). Selection lists are often displayed by the browser as a drop-down list or a scrollable list.

The size attribute determines how many options are shown without scrolling the list. If you set size="1", the control will give you a drop-down selection list when you click on it.

Selection of multiple items

Option groups for selection lists

Option Groups allow a developer to organize the options into labeled groups:

Practice: Select element on W3Schools

Reference: Select element on MDN

 

Option Buttons

Also known as radio buttons

The checked attribute specifies the default selection

Radio buttons can be grouped by the name attribute. Selection of buttons with the same name will be mutually exclusive.

Radio button practice on W3Schools

 

Access Keys

The accesskey attribute specifies a keyboard shortcut for moving the focus to a particular element.

To use the access key in Chrome, Edge or Safari, hold down the alt key while pressing the shortcut key. In Firefox, hold down the alt and shift keys while pressing the access key.

Access Key Practice on W3Schools

 

Input Validation

We can improve the way our form works and give our users a better experience, if we let them know when they have entered information that isn't valid for a particular form field. There are several ways we can do this using HTML. (There are even more ways to do this with JavaScript running on our web page or with other code running on a server.)

Validation using HTML5 input types

These <input> types are all essentially variants of the "text" type, but they limit the range of characters that the user is allowed to enter and/or change the appearance of the control to match the input type.

Code example:

Running in the browser:

Validation using the required attribute

Use the required attribute to ensure that the user enters something in a field.
Code example:

Running in the browser:

Validation using regular expressions

The pattern attribute allows us to use a special pattern-matching string called a regular expression, also called a regex.

Code example:

Running in the browser:

Here is the meaning of the symbols in the pattern attribute:

 Meaning of Regular Expression Symbols
^Start of the string.
\denotes that the charter following it is not a literal character and should be interpreted as special
\d{5}Match 5 digits
(?:…)Grouping
[-\s]Match a space or a hyphen
\d{4}Match 4 digits
?The pattern before it is optional
$End of the string

Regex references

 


Creative Commons License ASP.NET Core MVC Lecture Notes by Brian Bird is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.