Three attributes for better web forms

Forms on the web are an opportunity to make big improvements to the user experience with very little effort. The effort can be as little as sprinkling in a smattering of humble HTML attributes. But the result can be a turbo-charged experience for the user, allowing them to sail through their task.

This is particularly true on mobile devices where people have to fill in forms using a virtual keyboard. Any improvement you can make to their flow is worth investigating. But don’t worry: you don’t need to add a complex JavaScript library or write convoluted code. Well-written HTML will get you very far.

If you’re using the right input type value, you’re most of the way there. Browsers on mobile devices can use this value to infer which version of the virtual keyboard is best. So think beyond the plain text value, and use search, email, url, tel, or number when they’re appropriate.

But you can offer more hints to those browsers. Here are three attributes you can add to input elements. All three are enumerated values, which means they have a constrained vocabulary. You don’t need to have these vocabularies memorised. You can look them when you need to.

inputmode

The inputmode attribute is the most direct hint you can give about the virtual keyboard you want. Some of the values are redundant if you’re already using an input type of search, email, tel, or url.

But there might be occasions where you want a keyboard optimised for numbers but the input should also accept other characters. In that case you can use an input type of text with an inputmode value of numeric. This also means you don’t get the spinner controls on desktop browsers that you’d normally get with an input type of number. It can be quite useful to supress the spinner controls for numbers that aren’t meant to be incremented.

If you combine inputmode="numeric" with pattern="[0-9]", you’ll get a numeric keypad with no other characters.

The list of possible values for inputmode is text, numeric, decimal, search, email, tel, and url.

enterkeyhint

Whereas the inputmode attribute provides a hint about which virtual keyboard to show, the enterkeyhint attribute provides an additional hint about one specific key on that virtual keyboard: the enter key.

For search forms, you’ve got an enterkeyhint option of search, and for contact forms, you’ve got send.

The enterkeyhint only changes the labelling of the enter key. On some browsers that label is text. On others it’s an icon. But the attribute by itself doesn’t change the functionality. Even though there are enterkeyhint values of previous and next, by default the enter key will still submit the form. So those two values are less useful on long forms where the user is going from field to field, and more suitable for a series of short forms.

The list of possible values is enter, done, next, previous, go, search, and send.

autocomplete

The autocomplete attribute doesn’t have anything to do with the virtual keyboard. Instead it provides a hint to the browser about values that could pre-filled from the user’s browser profile.

Most browsers try to guess when they can they do this, but they don’t always get it right, which can be annoying. If you explicitly provide an autocomplete hint, browsers can confidently prefill the appropriate value.

Just think about how much time this can save your users!

There’s a name value you can use to get full names pre-filled. But if you have form fields for different parts of names—which I wouldn’t recommend—you’ve also got:

  • given-name,
  • additional-name,
  • family-name,
  • nickname,
  • honorific-prefix, and
  • honorific-suffix.

You might be tempted to use the nickname field for usernames, but no need; there’s a separate username value.

As with names, there’s a single tel value for telephone numbers, but also an array of sub-values if you’ve split telephone numbers up into separate fields:

  • tel-country-code,
  • tel-national,
  • tel-area-code,
  • tel-local, and
  • tel-extension.

There’s a whole host of address-related values too:

  • street-address,
  • address-line1,
  • address-line2, and
  • address-line3, but also
  • address-level1,
  • address-level2,
  • address-level3, and
  • address-level4.

If you have an international audience, addresses can get very messy if you’re trying to split them into separate parts like this.

There’s also postal-code (that’s a ZIP code for Americans), but again, if you have an international audience, please don’t make this a required field. Not every country has postal codes.

Speaking of countries, you’ve got a country-name value, but also a country value for the country’s ISO code.

Remember, the autocomplete value is specifically for the details of the current user. If someone is filling in their own address, use autocomplete. But if someone has specified that, say, a billing address and a shipping address are different, that shipping address might not be the address associated with that person.

On the subject of billing, if your form accepts credit card details, definitely use autocomplete. The values you’ll probably need are:

  • cc-name for the cardholder,
  • cc-number for the credit card number itself,
  • cc-exp for the expiry date, and
  • cc-csc for the security again.

Again, some of these values can be broken down further if you need them: cc-exp-month and cc-exp-year for the month and year of the expiry date, for example.

The autocomplete attribute is really handy for log-in forms. Definitely use the values of email or username as appropriate.

If you’re using two-factor authentication, be sure to add an autocomplete value of one-time-code to your form field. That way, the browser can offer to prefill a value from a text message. That saves the user a lot of fiddly copying and pasting. Phil Nash has more details on the Twilio blog.

Not every mobile browser offers this functionality, but that’s okay. This is classic progressive enhancement. Adding an autocomplete value won’t do any harm to a browser that doesn’t yet understand the value.

Use an autocomplete value of current-password for password fields in log-in forms. This is especially useful for password managers.

But if a user has logged in and is editing their profile to change their password, use a value of new-password. This will prevent the browser from pre-filling that field with the existing password.

That goes for sign-up forms too: use new-password. With this hint, password managers can offer to automatically generate a secure password.

There you have it. Three little HTML attributes that can help users interact with your forms. All you have to do was type a few more characters in your input elements, and users automatically get a better experience.

This is a classic example of letting the browser do the hard work for you. As Andy puts it, be the browser’s mentor, not its micromanager:

Give the browser some solid rules and hints, then let it make the right decisions for the people that visit it, based on their device, connection quality and capabilities.

This post has also been translated into French.

Have you published a response to this? :

Responses

Petit SEO

Adactio: Journal—Three attributes for better web forms adactio.com/journal/19842?… Forms on the web are an opportunity to make big improvements to the user experience with very little effort.

# Posted by Petit SEO on Friday, February 10th, 2023 at 3:49pm

trovster

🔗 Three attributes for better web forms > Forms on the web are an opportunity to make big improvements to the user experience with very little effort. The effort can be as little as sprinkling in a smattering of humble HTML attributes. adactio.com/journal/19842

# Posted by trovster on Monday, March 20th, 2023 at 6:09pm

Aaron T. Grogg

The View Transitions API is absolutely fascinating me… I cannot wait for better browser support… This will be such a game-changer for the web! Here are several articles and examples:

As of publication, you will need to view all live examples in a Chromium browser, and enable the following experimental flags:
chrome://flags#view-transition chrome://flags#view-transition-on-navigation

And speaking of examples of “what is possible”, whatpwacando.today demonstrates all the fancy features a PWA can do today!

Recommend opening on a mobile device as a lot of the features only work there.

Thanks to for this very clever HTML+CSS only “tree view” list. Basically, nesting uls within details within uls… then using some fancy CSS to make it look awesome… And NO JS!

Sticking with CSS for another second, shares this nice walk-through for creating a skewed background highlight using nothing but CSS.

Okay, just one more CSS… writes about some Lesser-Known And Underused CSS Features. You will likely know some, but some stand-outs for me are all (really like all: unset; within a specific selector!), currentColor (have used this a lot, but still forget about it!), Interaction Media Queries, isolation (Wow!), and, oh… scroll-padding

Somebody help me, I can’t stop… I think most of us probably at least familiar with the concept of CSS Logical Properties by now, those super-efficient new ways to define block, inline, direction, etc., so that it works in ltr and rtl reading environments? If not, will help get you up-to-speed. If so, it also works as a really nice cheatsheet!

Overlapping elements. We’ve all seen it, text over images or videos or graphics, and most of us have had to build it, typically absolutely-positioning something over something else, and always with some level of fragility at some breakpoint. But offers another approach by overlapping elements using display: grid!

And while we’re creating responsive components, let’s make sure our content is responsive by using this this clamp() calculator from !

And while we’re getting our responsive content in order, why not let help us get our vertical spacing straightened out, too?

And heck, why not let help us combine Custom Properties and responsive content with his Stack and Flow tips.

offers a thorough explainer on fetchpriority and how it can help LCP. The key that I think a lot of people miss regarding fetchpriority is that it doesn’t change the priority to “high”, for example, but requests it be higher than the default.

And shares a few more tips for getting your LCP in front of your users more quickly

Here is a slick accessible hamburger menu button that requires no JS. I would have added cursor: pointer to the label, but I realize this is mostly intended for touchscreens. Love it!

shares a couple of really useful form attributes we should all be adding to help our users. And if you are doing two-factor authentication, please take note of autocomplete="one-time-code", so the browser can offer to prefill the code once the text arrives. Also be sure to click through to the Phil Nash article that Jeremy links to to improve your two factor authentication experience.

And finally, offers his Debugging Tactics, a slightly tongue-in-cheek look at how best to approach our daily task of debugging something…

Happy reading, Atg

Related

14 Shares

# Shared by Curtiss Grymala (he/him/his) on Thursday, January 19th, 2023 at 12:35pm

# Shared by Derek P. Collins on Thursday, January 19th, 2023 at 12:35pm

# Shared by Luke Dary on Thursday, January 19th, 2023 at 12:35pm

# Shared by Damian Wajer on Thursday, January 19th, 2023 at 2:12pm

# Shared by Jon Lunman on Thursday, January 19th, 2023 at 2:12pm

# Shared by Fynn Becker on Thursday, January 19th, 2023 at 2:12pm

# Shared by Rob Ballou on Thursday, January 19th, 2023 at 3:09pm

# Shared by Charles Harries on Thursday, January 19th, 2023 at 6:44pm

# Shared by Andy Linton on Thursday, January 19th, 2023 at 7:25pm

# Shared by Brendon Walsh on Friday, January 20th, 2023 at 2:46am

# Shared by Curtis Wilcox on Friday, January 20th, 2023 at 5:36am

# Shared by Kashyap on Sunday, January 22nd, 2023 at 8:53am

# Shared by Matt Wing on Monday, January 23rd, 2023 at 8:26pm

# Shared by Jeff Sheets on Monday, January 23rd, 2023 at 10:31pm

20 Likes

# Liked by Thomas on Thursday, January 19th, 2023 at 12:02pm

# Liked by Luke Dary on Thursday, January 19th, 2023 at 12:35pm

# Liked by skipchris@mastodonapp.uk on Thursday, January 19th, 2023 at 12:35pm

# Liked by Stuart on Thursday, January 19th, 2023 at 12:35pm

# Liked by Derek P. Collins on Thursday, January 19th, 2023 at 12:35pm

# Liked by Jon Lunman on Thursday, January 19th, 2023 at 2:11pm

# Liked by Fynn Becker on Thursday, January 19th, 2023 at 2:12pm

# Liked by Richard Rutter on Thursday, January 19th, 2023 at 2:12pm

# Liked by Tom Brown🏒 on Thursday, January 19th, 2023 at 4:18pm

# Liked by Tim Murtaugh on Thursday, January 19th, 2023 at 4:18pm

# Liked by Charles Harries on Thursday, January 19th, 2023 at 6:44pm

# Liked by Rudiger Meyer on Thursday, January 19th, 2023 at 7:25pm

# Liked by seesee@social.dev-wiki.de on Thursday, January 19th, 2023 at 7:25pm

# Liked by Derek on Thursday, January 19th, 2023 at 8:20pm

# Liked by Marty McGuire on Friday, January 20th, 2023 at 2:26am

# Liked by Alan Dalton on Friday, January 20th, 2023 at 10:04am

# Liked by Phil Hawksworth on Friday, January 20th, 2023 at 12:36pm

# Liked by Chris Burnell on Saturday, January 21st, 2023 at 1:47am

# Liked by Harshad Sharma on Sunday, January 22nd, 2023 at 8:54am

# Liked by Kashyap on Sunday, January 22nd, 2023 at 8:54am

Related posts

Displaying HTML web components

You might want to use `display: contents` …maybe.

button invoketarget=”share”

An alternate route to a declarative version of the Web Share API.

Lost in calculation

A lazy option for responsive images is at hand.

HTML web components

Don’t replace. Augment.

Button types

Ideas for some declarative shortcuts.

Related links

A microdata enhanced HTML Webcomponent for Leaflet | k-nut — Blog

Here’s a nice HTML web component that uses structured data in the markup to populate a Leaflet map.

Personally I’d probably use microformats rather than microdata, but the princple is the same: progressive enhancement from plain old HTML to an interactive map.

Tagged with

drab

This looks like a handy collection of HTML web components for common interface patterns.

drab does not use the shadow DOM, so you can style content within these elements as usual with CSS.

Tagged with

HTML Web Components on the Server Are Great | Scott Jehl, Web Designer/Developer

Scott has written a perfect description of HTML web components:

They are custom elements that

  1. are not empty, and instead contain functional HTML from the start,
  2. receive some amount of progressive enhancement using the Web Components JavaScript lifecycle, and
  3. do not rely on that JavaScript to run for their basic content or functionality.

Tagged with

Light-DOM-Only Web Components are Sweet – Frontend Masters Boost

The main reason I’m so hot on Light DOM is that I find the styling story of Web Components using Shadow DOM annoying.

Tagged with

Apple Annie’s Weblog · Diving into indiewebify.me & microformats, a series

Here’s a good walkthrough of adding microformats to your site, starting with h-card and moving on to h-entry.

Tagged with

Previously on this day

6 years ago I wrote Heisenberg

JavaScript and the observer effect.

7 years ago I wrote Looking beyond launch

We’ll fix it in post.

8 years ago I wrote Paint

The power of art.

8 years ago I wrote Hamburger, hamburger, hamburger

Rigidly defined areas of doubt and uncertainty.

13 years ago I wrote Marklar Malkovich Smurf

Some links from ‘round the web on HTML5, HTML5, and also, HTML5.

19 years ago I wrote Delicious raw materials

It is no coincidence that industrial manufacturing plants aggregate close to sources of raw material. The North of England and the Ruhrgebiet in Germany were both rich sources of coal and centres of industry.

21 years ago I wrote Taking it easy

I’m still in Arizona. I’m just taking it easy, lounging around watching movies.

22 years ago I wrote Book round-up

Seeing as I’ve been issuing film reviews, I thought it would only be fair to go through some of the books I’ve read lately.