Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Jeremy Keith
Jeremy Keith

Posted on • Originally published atadactio.com on

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 rightinput 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 plaintext value, and usesearch,email,url,tel, ornumber when they’re appropriate.

But you can offer more hints to those browsers. Here are three attributes you can add toinput 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

Theinputmode 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 aninput type ofsearch,email,tel, orurl.

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 aninput type oftext with aninputmode value ofnumeric. This also means you don’t get the spinner controls on desktop browsers that you’d normally get with aninput type ofnumber. It can be quite useful to supress the spinner controls for numbers that aren’t meant to be incremented.

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

The list of possible values forinputmode istext,numeric,decimal,search,email,tel, andurl.

enterkeyhint

Whereas theinputmode attribute provides a hint about which virtual keyboard to show,theenterkeyhint attribute provides an additional hint about one specific key on that virtual keyboard: theenter key.

For search forms, you’ve got anenterkeyhint option ofsearch, and for contact forms, you’ve gotsend.

Theenterkeyhint 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 thefunctionality. Even though there areenterkeyhint values ofprevious andnext, 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 isenter,done,next,previous,go,search, andsend.

autocomplete

Theautocomplete 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 anautocomplete hint, browsers can confidently prefill the appropriate value.

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

There’s aname value you can use to get full names pre-filled. But if you have form fields for different parts of names—whichI 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 thenickname field for usernames, but no need; there’s a separateusername value.

As with names, there’s a singletel 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 alsopostal-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 acountry-name value, but also acountry value forthe country’s ISO code.

Remember, theautocomplete value is specifically for the details of the current user. If someone is filling in their own address, useautocomplete. 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 useautocomplete. 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 andcc-exp-year for the month and year of the expiry date, for example.

Theautocomplete attribute isreally handy for log-in forms. Definitely use the values ofemail orusername as appropriate.

If you’re using two-factor authentication, be sure to add anautocomplete value ofone-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 anautocomplete value won’t do any harm to a browser that doesn’t yet understand the value.

Use anautocomplete value ofcurrent-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 tochange their password, use a value ofnew-password. This will prevent the browser from pre-filling that field with the existing password.

That goes for sign-up forms too: usenew-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 yourinput 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.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

¯\_(ツ)_/¯
  • Location
    Brighton, UK
  • Work
    Web Stuff Do-er at Clearleft
  • Joined

More fromJeremy Keith

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp