亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Table of Contents
Search (Search)
Email Addresses (Email Addresses)
URL
Verification of URL
Telephone Numbers (Telephone Numbers)
FAQs for HTML5 Form Input Types (FAQs)
What are the different types of input types of HTML5 forms?
How does the "date" input type in HTML5 work?
What happens when viewing HTML5 form input types in older browsers?
How to verify user input in HTML5 form?
What is the "range" input type in HTML5?
How does the "email" input type in HTML5 enhance the user experience?
What is the purpose of the "search" input type in HTML5?
How to use the "number" input type in HTML5?
What is the "tel" input type in HTML5 for?
How does the "url" input type in HTML5 work?
Home Web Front-end CSS Tutorial HTML5 Forms: Input Types (Part 1) - SitePoint

HTML5 Forms: Input Types (Part 1) - SitePoint

Feb 18, 2025 am 08:28 AM

Detailed explanation and best practices for HTML5 form input types

HTML5 Forms: Input Types (Part 1) - SitePoint

Core points

  • HTML5 forms introduce new input types, such as email, search, date, time, number, range, color, etc. These types provide user interface elements and native data verification functions that are more consistent with data types. Even in older browsers, these new input types work properly, except that they will appear as standard text fields by default.
  • search input type (type="search") provides a search field to provide users with intuitive search site prompts. It usually comes with a built-in clear button and can be styled to match the search box of the browser or operating system.
  • email input type (type="email") is used to specify one or more email addresses. It supports the Boolean multiple attribute for multiple comma-separated email addresses. Touchpad devices usually display keyboards optimized for email input when this field gets focus, and some browsers also provide error messages for invalid email input.
  • url input type (type="url") is used to specify a network address, similar to the email input type, which displays as a normal text field, but provides a keyboard optimized for network address input on the touch screen. Modern browsers use this input type to verify the general protocol format of the URL.

(The following is excerpted from the book "HTML5 & CSS3 for the Real World, 2nd Edition" co-authored by Alexis Goldstein, Louis Lazaris and Estelle Weyl. The book is available in stores around the world, and you can also find it here. Purchase the e-book version. )

You may already be familiar with the input attributes of the type element. This property determines which type of form input will be presented to the user. If this property is omitted—or for new input types and older browsers, if the browser does not understand the type—it remains valid; the input will default to type="text". This is what makes HTML5 forms still work today, even if you are still supporting older browsers. If you use a new input type, such as email or search, the older browser will only display standard text fields to the user.

Our registration form currently uses four of the ten input types you are familiar with: checkbox, text, password, and submit. Here is a list of all types available before HTML5:

  • button
  • checkbox
  • file
  • hidden
  • image
  • password
  • radio
  • reset
  • submit
  • text

HTML5 specification provides us with nine new input types that provide UI elements and native data verification capabilities that are more data-type-compliant:

  • search
  • email
  • url
  • tel
  • date
  • time
  • number
  • range
  • color

HTML5.1 and WHATWG HTML Living Standard contain four additional date input types, three of which are well supported in modern browsers:

  • datetime-local
  • month
  • week
  • datetime (no browser supports it)

Let's learn more about each new type and how to use them.

search input type (type="search") provides a search field - a single-line text input control for entering one or more search terms. Specification points out:

The difference between text state and search state is mainly in style: On platforms where the search field is different from the normal text field, the search state may cause the appearance to be consistent with the platform's search field, rather than like the normal text field .

Many browsers style search inputs in a consistent manner as the search box of the browser or operating system. Currently, Chrome, Safari, Opera, and IE have added the function of clearing input by clicking the mouse to provide the × icon after entering text, as shown in Figure 4.5. The date/time input type can also be cleared in Chrome and Opera. IE11 now also includes a × icon to clear most input types, including input with type text.

HTML5 Forms: Input Types (Part 1) - SitePoint

Figure 4.5. The style of the search input type is similar to the search field of the operating system

On Apple devices, the search fields in Chrome, Safari, and Opera have rounded corners by default, matching the device's search field appearance. On a touchpad with a dynamic keyboard, the “go” button will appear as the search icon or the word “search”, depending on the device. If you include non-standard results properties, Chrome and Opera will display the magnifying glass/find icon in the form field.

While you can still use type="text" to create search fields, the new search type can intuitively prompt users where to search for sites and provide an interface for users to be accustomed to. HTML5 HeraldThere is no search field, but here is an example of how to use it:

<form id="search" method="get">
  <label for="s">Search:</label>
  <input type="search" id="s" name="s"/>
  <input type="submit" value="Search"/>
</form>

Since search, like all new input types, appears as a regular text box in unsupported browsers, there is no reason not to use it when appropriate.

Email Addresses (Email Addresses)

email type (type="email") is used to specify one or more email addresses. It supports Boolean multiple attributes, allowing multiple comma-separated (optional spaces) email addresses. Let's change the form to use type="email" for the registrant's email address:

<form id="search" method="get">
  <label for="s">Search:</label>
  <input type="search" id="s" name="s"/>
  <input type="submit" value="Search"/>
</form>

If you change the input type from text to email, as we do here, you will notice that there is no noticeable change in the user interface; the input still looks like a normal text field. However, there are differences behind the scenes.

If you are using a touchpad device, this change becomes obvious. When you focus on the email field, most touchpad devices, such as iPads or Android phones running Chromium, display keyboards optimized for email input, including @ symbols, periods, and space buttons, but not commas, As shown in Figure 4.6.

HTML5 Forms: Input Types (Part 1) - SitePoint

Figure 4.6. Email input type provides a custom keyboard on iOS device

Firefox, Chrome, Opera, and Internet Explorer 10 also provides error messages for invalid email input: If you try to submit a form that does not recognize as one or more email addresses, the browser will tell you what's wrong . The default error message is shown in Figure 4.7.

HTML5 Forms: Input Types (Part 1) - SitePoint

Figure 4.7. Error message for incorrectly formatted email addresses on Opera (left) and Firefox (right)

Note: Custom verification messages do not like the default error messages provided by the browser? Use .setCustomValidity(errorMsg) to set your own message. setCustomValidity only accepts one parameter, the error message you want to provide. If you set up a custom validation message, once the value becomes a valid value, you must set the validation message to an empty string (false value) to enable form submission:

<label for="email">My email address is:</label>
<input type="email" id="email" name="email"/>

Unfortunately, while you can change the content of the message, at least for the time being you are still limited by its appearance.

URL

url input (type="url") is used to specify the network address. Much like email, it will appear as a normal text field. On many touch screens, the displayed on-screen keyboard will be optimized for network address input, with forward slashes (/) and ".com" shortcuts. Let's update our registration form to use the url input type:

function setErrorMessages(formControl) {
  var validityState_object = formControl.validity;
  if (validityState_object.valueMissing) {
      formControl.setCustomValidity('Please set an age (required)');  
  } else if (validityState_object.rangeUnderflow) {
      formControl.setCustomValidity('You\'re too young');
  } else if (validityState_object.rangeOverflow) {
      formControl.setCustomValidity('You\'re too old');
  } else if (validityState_object.stepMismatch) {
      formControl.setCustomValidity('Counting half birthdays?');
  } else {
      //如果有效,必須設(shè)置虛假值,否則將始終出錯
      formControl.setCustomValidity('');
  }
}

Verification of URL

All modern browsers starting with Internet Explorer 10 support url input types, and if the value does not start with a protocol, the input will be reported to be invalid. Only validate the general protocol format of the URL, so for example, q://example.xyz will be considered valid, even if q:// is not a real protocol, .xyz is not a real top-level domain. If you want the entered value to fit a more specific format, provide information in the tag (or placeholder) to inform the user and use the pattern attribute to ensure its correctness, as mentioned earlier.

Telephone Numbers (Telephone Numbers)

For phone numbers, use tel input type (type="tel"). Unlike url and email types, tel types do not enforce specific syntax or pattern. Letters and numbers—actually, any character except line breaks or carriage return—are valid. This has a good reason: around the world, every country has valid phone numbers of various lengths and punctuation, so it is impossible to specify a single format as a standard. For example, in the United States, 1(415)555-1212 is as easy to understand as 415.555.1212, but companies may also use letters in phone numbers, such as (800)CALL-NOW. You can encourage specific formatting by including placeholders with correct syntax or comments after input, along with examples. Additionally, you can specify the format using the pattern attribute. Include a pattern in the title attribute to provide tooltips and improve the user experience of native verification error messages. You can also use the setCustomValidity method to provide more informative client verification. When using the tel input type, the dynamic touchpad usually displays the phone keyboard, including the asterisk and pound keys. You can use tel for other purposes than phone numbers. For example, it may be the best keyboard for social security number form input.

FAQs for HTML5 Form Input Types (FAQs)

What are the different types of input types of HTML5 forms?

HTML5 introduces various new form input types. These include "color", "date", "datetime-local", "email", "month", "number", "range", "search", "tel", "time", "url" and "week ”. Each input type has its specific purpose and can greatly enhance the user experience of the website by providing more appropriate and user-friendly input fields.

How does the "date" input type in HTML5 work?

The "date" input type in HTML5 creates an input field that allows the user to enter a date. The date format is formatted according to the user's browser locale and a date selector is usually provided for user convenience. However, support for this input type varies from browser to browser.

What happens when viewing HTML5 form input types in older browsers?

If you view the HTML5 form input type in an older browser that does not support it, the browser will default to the standard "text" input field. This means that the user can still enter information, but specific features of the HTML5 input type (such as the date selector for the "date" input type) will not be available.

How to verify user input in HTML5 form?

HTML5 provides multiple attributes for input verification, including "required", "pattern", and "min"/"max" of numeric input types. These properties can be used to ensure that user input meets specific criteria before the form is submitted.

What is the "range" input type in HTML5?

The "range" input type in HTML5 creates a slider that allows the user to select values ??within a specific range. You can specify the range using the "min" and "max" properties and specify the default value using the "value" properties.

How does the "email" input type in HTML5 enhance the user experience?

The "email" input type in HTML5 provides a convenient way for users to enter their email address. It includes built-in verification to check if the entered text is in the email address format. Some browsers may also provide autocomplete for this input type.

What is the purpose of the "search" input type in HTML5?

The "search" input type in HTML5 is designed for search fields. This input type can provide search-specific features, such as clearing the search box with one click, or showing the most recent search results to the user.

How to use the "number" input type in HTML5?

The "number" input type in HTML5 allows the user to enter digital input. You can use the min and max properties to specify acceptable range of numbers and use the 'step' property to specify step values.

What is the "tel" input type in HTML5 for?

The "tel" input type in HTML5 is used for the input field that should contain the phone number. However, it does not verify the input, so you should use JavaScript or similar techniques for verification.

How does the "url" input type in HTML5 work?

The "url" input type in HTML5 is used for input fields that should contain URLs. It includes built-in verification to check if the entered text is in URL format. Some browsers may also provide autocomplete for this input type.

The above is the detailed content of HTML5 Forms: Input Types (Part 1) - SitePoint. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72
CSS tutorial for creating loading spinners and animations CSS tutorial for creating loading spinners and animations Jul 07, 2025 am 12:07 AM

There are three ways to create a CSS loading rotator: 1. Use the basic rotator of borders to achieve simple animation through HTML and CSS; 2. Use a custom rotator of multiple points to achieve the jump effect through different delay times; 3. Add a rotator in the button and switch classes through JavaScript to display the loading status. Each approach emphasizes the importance of design details such as color, size, accessibility and performance optimization to enhance the user experience.

Addressing CSS Browser Compatibility issues and prefixes Addressing CSS Browser Compatibility issues and prefixes Jul 07, 2025 am 01:44 AM

To deal with CSS browser compatibility and prefix issues, you need to understand the differences in browser support and use vendor prefixes reasonably. 1. Understand common problems such as Flexbox and Grid support, position:sticky invalid, and animation performance is different; 2. Check CanIuse confirmation feature support status; 3. Correctly use -webkit-, -moz-, -ms-, -o- and other manufacturer prefixes; 4. It is recommended to use Autoprefixer to automatically add prefixes; 5. Install PostCSS and configure browserslist to specify the target browser; 6. Automatically handle compatibility during construction; 7. Modernizr detection features can be used for old projects; 8. No need to pursue consistency of all browsers,

What is the difference between display: inline, display: block, and display: inline-block? What is the difference between display: inline, display: block, and display: inline-block? Jul 11, 2025 am 03:25 AM

Themaindifferencesbetweendisplay:inline,block,andinline-blockinHTML/CSSarelayoutbehavior,spaceusage,andstylingcontrol.1.Inlineelementsflowwithtext,don’tstartonnewlines,ignorewidth/height,andonlyapplyhorizontalpadding/margins—idealforinlinetextstyling

Styling visited links differently with CSS Styling visited links differently with CSS Jul 11, 2025 am 03:26 AM

Setting the style of links you have visited can improve the user experience, especially in content-intensive websites to help users navigate better. 1. Use CSS's: visited pseudo-class to define the style of the visited link, such as color changes; 2. Note that the browser only allows modification of some attributes due to privacy restrictions; 3. The color selection should be coordinated with the overall style to avoid abruptness; 4. The mobile terminal may not display this effect, and it is recommended to combine it with other visual prompts such as icon auxiliary logos.

Creating custom shapes with css clip-path Creating custom shapes with css clip-path Jul 09, 2025 am 01:29 AM

Use the clip-path attribute of CSS to crop elements into custom shapes, such as triangles, circular notches, polygons, etc., without relying on pictures or SVGs. Its advantages include: 1. Supports a variety of basic shapes such as circle, ellipse, polygon, etc.; 2. Responsive adjustment and adaptable to mobile terminals; 3. Easy to animation, and can be combined with hover or JavaScript to achieve dynamic effects; 4. It does not affect the layout flow, and only crops the display area. Common usages are such as circular clip-path:circle (50pxatcenter) and triangle clip-path:polygon (50%0%, 100 0%, 0 0%). Notice

What is the CSS Painting API? What is the CSS Painting API? Jul 04, 2025 am 02:16 AM

TheCSSPaintingAPIenablesdynamicimagegenerationinCSSusingJavaScript.1.DeveloperscreateaPaintWorkletclasswithapaint()method.2.TheyregisteritviaregisterPaint().3.ThecustompaintfunctionisthenusedinCSSpropertieslikebackground-image.Thisallowsfordynamicvis

How to create responsive images using CSS? How to create responsive images using CSS? Jul 15, 2025 am 01:10 AM

To create responsive images using CSS, it can be mainly achieved through the following methods: 1. Use max-width:100% and height:auto to allow the image to adapt to the container width while maintaining the proportion; 2. Use HTML's srcset and sizes attributes to intelligently load the image sources adapted to different screens; 3. Use object-fit and object-position to control image cropping and focus display. Together, these methods ensure that the images are presented clearly and beautifully on different devices.

What are common CSS browser inconsistencies? What are common CSS browser inconsistencies? Jul 26, 2025 am 07:04 AM

Different browsers have differences in CSS parsing, resulting in inconsistent display effects, mainly including the default style difference, box model calculation method, Flexbox and Grid layout support level, and inconsistent behavior of certain CSS attributes. 1. The default style processing is inconsistent. The solution is to use CSSReset or Normalize.css to unify the initial style; 2. The box model calculation method of the old version of IE is different. It is recommended to use box-sizing:border-box in a unified manner; 3. Flexbox and Grid perform differently in edge cases or in old versions. More tests and use Autoprefixer; 4. Some CSS attribute behaviors are inconsistent. CanIuse must be consulted and downgraded.

See all articles