{"id":5234,"date":"2013-09-07T10:00:06","date_gmt":"2013-09-07T14:00:06","guid":{"rendered":"http:\/\/www.ericfeminella.com\/blog\/?p=5234"},"modified":"2015-12-14T15:08:26","modified_gmt":"2015-12-14T20:08:26","slug":"ios-html5-input-element-tips","status":"publish","type":"post","link":"https:\/\/www.ericfeminella.com\/blog\/2013\/09\/07\/ios-html5-input-element-tips\/","title":{"rendered":"HTML5 Input Elements on iOS"},"content":{"rendered":"<p>Perhaps some of the most important UX considerations to make are those surrounding the simplicity with which <a href=\"http:\/\/www.frontend.com\/websites-portals-ecommerce\/tips-for-creating-online-forms.html\" target=\"_blank\">forms can be completed<\/a>. This is especially important when taking into account the <a href=\"http:\/\/www.lukew.com\/ff\/entry.asp?1333\" target=\"_blank\">constraints of Mobile devices<\/a>.<\/p>\n<h1>Input Elements and the iOS Keyboard<\/h1>\n<p>While implementing a form for a Mobile Web Application, I found myself in need of a way to control some of the default behaviors of the native <a href=\"http:\/\/developer.apple.com\/library\/ios\/documentation\/StringsTextFonts\/Conceptual\/TextAndWebiPhoneOS\/KeyboardManagement\/KeyboardManagement.html\" target=\"_blank\">iOS Keyboard<\/a>. Specifically, I found it rather inconvenient on the user&#8217;s part to require manual closing of the keyboard of any kind, especially after submitting a form. I also found it inconvenient to have to manually turn off auto capitalization on input elements, or having to work around the default auto-correct behavior on input elements. <\/p>\n<p>Fortunately, <a href=\"https:\/\/developer.apple.com\/library\/safari\/#codinghowtos\/Mobile\/UserExperience\/_index.html\" target=\"_blank\">these issues<\/a> (as well as others) have solutions which are readily available, both natively and programmatically.<\/p>\n<h2>Turning off auto-caps<\/h2>\n<p>By default, the iOS Keyboard displays with Caps Lock on for the first charachter on input elements of type <em>text<\/em>. For certain use-cases, such as entering usernames, this may not be desirable.<\/p>\n<p>Caps Lock can be turned off by simply defining an <code>autocapitalize<\/code> attribute with a value of <code>off<\/code> on input elements:<\/p>\n<pre lang=\"html\">\n<!-- disable auto-capitalize -->\n<input type=\"text\" autocapitalize=\"off\">\n<\/pre>\n<h2>Turning off auto-correct<\/h2>\n<p>As with with Caps Lock, in iOS, by default, input elements of type <em>text<\/em> have auto-correct enabled. For certain use-cases, again, such as entering usernames, this may not be desirable.<\/p>\n<p>Auto-correct can be disabled by simply defining an <code>autocorrect<\/code> attribute with a value of <code>off<\/code> on input elements:<\/p>\n<pre lang=\"html\">\n<!-- disable auto-correct -->\n<input type=\"text\" autocorrect=\"off\">\n<\/pre>\n<h2>Automatically closing the Keyboard<\/h2>\n<p>When submitting a form, at times, the iOS Keyboard may not automatically close. This is quite a usability issue as Users should not be required to manually close the Keyboard for use-cases in which they would otherwise not expect the need do so.<\/p>\n<p>A simple solution for this can be implemented by invoking the <a href=\"http:\/\/www.whatwg.org\/specs\/web-apps\/current-work\/#dom-blur\" target=\"_blank\">blur method<\/a> on <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/DOM\/document.activeElement\" target=\"_blank\">document.activeElement<\/a>, which effectively allows one to programmatically hide the keyboard:<\/p>\n<pre lang=\"js\">\n\/\/ automatically close the keyboard on iOS\ndocument.activeElement.blur();\n...\n<\/pre>\n<h2>HTML5 Input Attribute Types<\/h2>\n<p>In addition to controlling the default behavior of the iOS Keyboard, specific types of Keyboards can be invoked simply by defining a supported <a href=\"http:\/\/www.w3.org\/TR\/html-markup\/input.html\" target=\"_blank\">HTML5 input element type<\/a>. <\/p>\n<p>The following examples demonstrate just how easy it is to display a context specific keyboard: <\/p>\n<p>The Email keyboard can be invoked via the <a href=\"http:\/\/www.w3.org\/TR\/html-markup\/input.email.html#input.email\" target=\"_blank\">email input type<\/a>:<\/p>\n<pre lang=\"html\">\n<!-- input element of type 'email' displays the iOS email keyboard -->\n<input type=\"email\" placeholder=\"email keyboard\" autocorrect=\"off\">\n<\/pre>\n<p>The URL keyboard can be invoked via the <a href=\"http:\/\/www.w3.org\/TR\/html-markup\/input.url.html#input.url\" target=\"_blank\">url input type<\/a>:<\/p>\n<pre lang=\"html\">\n<!-- input element of type 'url' displays the iOS URL keyboard -->\n<input type=\"url\" placeholder=\"url keyboard\" autocorrect=\"off\">\n<\/pre>\n<p>The Telephone keyboard can be invoked via the <a href=\"http:\/\/www.w3.org\/TR\/html-markup\/input.tel.html#input.tel\" target=\"_blank\">tel input type<\/a>:<\/p>\n<pre lang=\"html\">\n<!-- input element of type 'tel' displays the iOS telephone keyboard -->\n<input type=\"tel\" placeholder=\"telephone keyboard\" autocorrect=\"off\">\n<\/pre>\n<p>The Numeric keyboard can also be invoked via the <a href=\"http:\/\/www.w3.org\/TR\/html-markup\/datatypes.html#form.data.pattern-def\" target=\"_blank\">pattern attribute<\/a>, the value of which being either of the following Regular Expressions <code>[0-9]*<\/code> or <code>\\d*<\/code>:<\/p>\n<pre lang=\"html\">\n<!-- input element of type 'text' with pattern attribute displays the iOS numeric keyboard -->\n<input type=\"text\" placeholder=\"numeric keyboard\" pattern=\"[0-9]*\" \/>\t \n<\/pre>\n<p>You can try the above examples <a href=\"http:\/\/code.ericfeminella.com\/articles\/examples\/ios-keyboard-mobile-web-example\" target=\"_blank\">here<\/a>, or view the <a href=\"https:\/\/gist.github.com\/4394637\" target=\"_blank\">gist<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Perhaps some of the most important UX considerations to make are those surrounding the simplicity with which forms can be completed. This is especially important when taking into account the constraints of Mobile devices. Input Elements and the iOS Keyboard While implementing a form for a Mobile Web Application, I found myself in need of a way to control some&#8230; <a class=\"read-more\" href=\"https:\/\/www.ericfeminella.com\/blog\/2013\/09\/07\/ios-html5-input-element-tips\/\">Continue Reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[133,59,201,112],"tags":[],"class_list":["post-5234","post","type-post","status-publish","format-standard","hentry","category-browsers","category-html5","category-quick-tips","category-user-experience-design"],"jetpack_publicize_connections":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/posts\/5234","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/comments?post=5234"}],"version-history":[{"count":0,"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/posts\/5234\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/media?parent=5234"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/categories?post=5234"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/tags?post=5234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}