一个新的HTML5表单Inpu指南t Types

President of WebFX. Bill has over 25 years of experience in the Internet marketing industry specializing in SEO, UX, information architecture, marketing automation and more. William's background in scientific computing and education from Shippensburg and MIT provided the foundation for MarketingCloudFX and other key research and development projects at WebFX.

There’s a plethora of newHTML5 form input types(13 new ones to be exact) that make creating engaging and easy-to-use web forms much easier for web designers. The new HTML5 input types give us data validation, date picker controls, color picker controls, inline help text, and more in the web browsers that support them.

Advantage of HTML5 Form Input Types

The benefits of these new input types are tremendous for web designers.

First, the new input types reduce our reliance on client-side- and server-side-scripting for validating common data types like dates, email addresses, and URLs. For mobile UI developers: You know that creating cross-platform web forms using HTML4 standards is a pain. Using HTML5 form input types could make things easier.

For instance, a form written with HTML5 can utilize the mobile device’s native specialized keyboards depending on what the target input type is. Here’s an example of using HTML4 input types (on the left) for entering dates in a web form versus using the HTML5dateinput type (on the right):The difference between using HTML4 versus the HTML5 date input type.Notice how the iPhone’s native keyboard automatically switches over to its native date picker control because it knows that the target input is a date. Here are other examples.

url Input Type

email Input Type

The New HTML5 Form Input Types

Here’s a table of all the new HTML5 input types for your reference.

Input Type Description HTML Markup
date A control for entering the date.
datetime Date and time usingUTC date and time format
datetime-local Date and time according to your local time
month Month and year
time 一天的时间
week Allows you to pick the week and year.
color Allows you to enter a simple color value (which is in hexadecimal color notation)
email Validates the input using the standard email format
tel Gives you the ability to validate telephone numbers format against a pattern
search Searches a data set (like aHTML element)
range A slider control for picking a number in between two numbers
number Accepts numbers only
url Accepts URLs only

HTML5 Form Input Types Demo

Here’s a live demo of each input type so that you can play around with them.Note:This demo page will only work on browsers that support these input types, so it mightnot work properlywhen you view it.

We also recommend viewing this demo page on different browsers so you can see how each browser treats these input types; they differ drastically in some cases.

Let’s talk about the new input types.

date

dateThecalendar date pickeris a standard UI design pattern for allowing users to enter date information in web forms.

这是对要求用户输入他们的dat有用e of birth or credit card expiration date. This UI pattern is so common that in HTML5, there’s now thedateinput type that makes it dead-simple to include date pickers in your web forms. No more JavaScript or server-side programming or installing jQuery plugins, you just need HTML markup (and CSS to make it look prettier).

datetime

datetimeThis input type is designed to take in a global date and time string.

datetime-local

datetime-localThis input type is for a local date and time string.

month

monthIf you want to allow users to enter a month, this is the input type to use.

week

weekIf you want to allow users to enter a week (such as the 31st week of 2013) this is the input type to use.

time

timeThis is for inputting the time in the the day. Some browsers will use military time by default (e.g.

16:00), while others use the AM/PM format (e.g. 4:00PM).

color

colorThe color picker UI design pattern is a way of allowing users to choose a color.

The color input type gives you a form input control for a picking hexadecimal color value. Some browsers (like Safari) won’t show you a color picker UI though, they’ll just check whether or not the data is a hexadecimal color code (e.g. #000000).

email

emailIn HTML5-compatible browsers, this input type is set to validate the entered data for the correct email address syntax.

tel

telThis input type is ideal for taking in phone numbers.

search

searchThe search input type is meant to be used with a data set such as the datalist HTML element or the results of querying a MySQL database.

It searches the data set for a match, and also gives you a list of suggestions as you type (which is called theautocompleteuser interface pattern).

range

rangeThis input type is another way to get number-related data from users. By default, it has a slider user interface.

number

numberThis input type restricts data entries only to numerical values. If you includeminandmaxattributes, it also validates the data input to make sure the number is between the min/max values.

url

urlTheis input type validates the entered data to make sure that it follows the standard URL format.

Conclusion

These additional input types in HTML5 help us build user-centered web forms easier. The modern web designer no longer needs to break his/her back in order to enable more complex data input patterns. The times are changing for all of us.

Let’s not forget to update our forms to match.

Related Content

WebFX Careers

Join our mission to provide industry-leading digital marketing services to businesses around the globe - all while building your personal knowledge and growing as an individual.

We’re Hiring! View30+job openings!