{"id":3451,"date":"2015-11-06T17:41:37","date_gmt":"2015-11-06T23:41:37","guid":{"rendered":"http:\/\/neosmart.net\/blog\/?p=3451"},"modified":"2015-11-06T17:44:43","modified_gmt":"2015-11-06T23:44:43","slug":"a-smarter-month-and-year-input-field","status":"publish","type":"post","link":"https:\/\/neosmart.net\/blog\/a-smarter-month-and-year-input-field\/","title":{"rendered":"A smarter month and year input field: solving the credit card expiry conundrum"},"content":{"rendered":"<p>Every computer-savvy keyboarder has run into this problem before: you&#8217;re typing your merry way through a credit card payment form, pounding out your address and credit card numbers, tabbing between the fields so you don&#8217;t waste time mucking around with the mouse, when you come across the dreaded expiry date fields:<\/p>\n<p><a href=\"\/blog\/wp-content\/uploads\/Expiry-Date-Fields.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-3452 colorbox-3451\" src=\"\/blog\/wp-content\/uploads\/Expiry-Date-Fields.png\" alt=\"Expiry Date Fields\" width=\"305\" height=\"147\" srcset=\"https:\/\/neosmart.net\/blog\/wp-content\/uploads\/Expiry-Date-Fields.png 610w, https:\/\/neosmart.net\/blog\/wp-content\/uploads\/Expiry-Date-Fields-500x241.png 500w\" sizes=\"auto, (max-width: 305px) 100vw, 305px\" \/><\/a><\/p>\n<p>You groan. You know what&#8217;s coming next. Your card expires in April of 2019. Are they expecting you to type &#8216;A&#8217; for &#8220;April&#8221; or to key in &#8220;04&#8221;? Or maybe it&#8217;s just &#8216;4&#8217;? Murphy&#8217;s law guarantees\u00a0that whatever sequence you try typing these options in, it&#8217;ll be the last one you try. You sigh. You either try the different options haphazardly, holding your breath and cringing when it doesn&#8217;t change, or changes to select the wrong value. Finally, you give up and move your hand those excruciatingly-far 6&#8243; to the mouse, and sigh in despair as you resort to clicking on the drop-down box and scrolling through the entries to select the one you&#8217;re looking for.<\/p>\n<p><!--more--><\/p>\n<p>What if I told you there was a better way? And, no, it&#8217;s not doing something like graying-out some sample placeholder text and hoping your customers won&#8217;t be confused, thinking that was a pre-selected month\/year. Here, try it out yourself. Just click in the first input field (&#8220;Just a place for you to start tabbing&#8230;&#8221;) then &lt;tab&gt; your way across the rest of the form and key your choice of month and year, in <em>any<\/em> format you like. The form will magically change itself based on who you are and what kind of preferences you have, and it&#8217;ll change accordingly to make sure that whatever you end up typing will work!<\/p>\n<p><iframe width=\"100%\" src=\"\/\/jsfiddle.net\/mqudsi\/3u1x2pue\/embedded\/result\/\" allowfullscreen=\"allowfullscreen\" frameborder=\"0\" style=\"height: 150px\"><\/iframe><\/p>\n<p>Oh my gosh! How did that work? Well, it didn&#8217;t really read your mind and it&#8217;s actually not magic (even if it seems that way) (and despite what I said before). It&#8217;s quite simple: the list actually contains all the variations of the month and year formats:<\/p>\n<p><script src=\"https:\/\/gist.github.com\/mqudsi\/129799041682fb3a3531.js\"><\/script><\/p>\n<p>Then, with some clever javascript magic, we match up whatever the user selected to the &#8220;proper&#8221; format we wish the dropdown menu to show when they navigate away from the entry field. But it would be <em>extremely<\/em> confusing to less tech-savvy users if their drop-down contained multiple options evaluating to the same month &#8211; when you have to choose between &#8220;January&#8221;, &#8220;01&#8221;, and &#8220;1&#8221;, which do you choose? So the javascript hides the not &#8220;proper&#8221; options each time the drop-down menu is opened (via clicking or by hitting the spacebar) and returns the values when the menu is closed once more!<\/p>\n<p>Here&#8217;s the full javascript code to pull this off. It&#8217;s using jquery, but there&#8217;s absolutely nothing complicated in there and it can be easily rewritten to use plain old javascript instead (it&#8217;s a GitHub gist, so anyone willing to do so, fork away!):<\/p>\n<p><script src=\"https:\/\/gist.github.com\/mqudsi\/6cec1827ef931eed69d0.js\"><\/script><\/p>\n<p>Each time the menu is opened, any &lt;option&gt; tagged with the class &#8220;extraneous&#8221; is detached from the parent &lt;select&gt; and stored in an array associated with the self-same &lt;select&gt;. Each time the user navigates away from the &lt;select&gt;, the drop-down menu is re-populated with the previously-removed elements. It works like magic on any &lt;select&gt; tagged with the class &#8220;magic&#8221; and should be fairly reliable! We&#8217;ve been using this code on the checkout page in <a href=\"\/Store\/\">both<\/a> our <a href=\"\/EasyRE\/\">online stores<\/a> for the past few years and it&#8217;s been working great. Well enough, in fact, that we decided to share it with everyone!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Every computer-savvy keyboarder has run into this problem before: you&#8217;re typing your merry way through a credit card payment form, pounding out your address and credit card numbers, tabbing between the fields so you don&#8217;t waste time mucking around with &hellip; <a href=\"https:\/\/neosmart.net\/blog\/a-smarter-month-and-year-input-field\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":505,"featured_media":0,"comment_status":"open","ping_status":"closed","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":""},"categories":[1],"tags":[76,11,430,905,177],"class_list":["post-3451","post","type-post","status-publish","format-standard","hentry","category-software","tag-javascript","tag-programming","tag-user-interface","tag-ux","tag-web-development"],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4xDa-TF","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts\/3451","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/users\/505"}],"replies":[{"embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/comments?post=3451"}],"version-history":[{"count":27,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts\/3451\/revisions"}],"predecessor-version":[{"id":3479,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts\/3451\/revisions\/3479"}],"wp:attachment":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/media?parent=3451"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/categories?post=3451"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/tags?post=3451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}