{"id":19051,"date":"2012-10-30T06:34:54","date_gmt":"2012-10-30T13:34:54","guid":{"rendered":"http:\/\/css-tricks.com\/?p=19051"},"modified":"2012-10-30T06:34:54","modified_gmt":"2012-10-30T13:34:54","slug":"display-form","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/display-form\/","title":{"rendered":"Display Form <progress>"},"content":{"rendered":"

Imagine you have a form with 10 required fields on it. That’s a bit larger than your average form on the internet and a good bit of work for a user. You might lose some users along the way, which is never a good thing. Perhaps to encourage finishing the form, you let them know how close to completion they are, and display motivational messages along the way.<\/p>\n

<\/p>\n

progressform
View Demo<\/a><\/figcaption><\/figure>\n

1. HTML for Required Inputs<\/h3>\n

Inputs wouldn’t necessarily need to be required<\/code> for this demo, but it makes for a clean demo. Filling out an optional field doesn’t bring you any closer to the minimum requirements for submitting a form. So for this demo, we’ll have 5 required fields down a typical form that collects information for payment. <\/p>\n

<form id=\"payment-form\" action=\"#\">\r\n  \r\n   <label for=\"name\">Full Name<\/label>\r\n   <input required id=\"name\" name=\"name\">\r\n\r\n   <!-- etc -->\r\n\r\n<\/form><\/code><\/pre>\n

2. HTML for Progress<\/h3>\n

There is an HTML element specifically for visually displaying progress. Literally, <progress><\/code>. Let’s have one of these available (with an ID so it’s easy to target with JavaScript). Place it where you will.<\/p>\n

<progress max=\"100\" value=\"0\" id=\"progress\"><\/progress><\/code><\/pre>\n

Making the max<\/code> attribute 100 means for easy math. The progress bar is as full as the value in percentage is. E.g. value=\"20\"<\/code> == bar is 20% full.<\/p>\n

progress
No styling needed, you get this look by default (platform specific).<\/figcaption><\/figure>\n

3. Watch for Changes<\/h3>\n

We’ll use jQuery here to make event binding and element selection easy. All our inputs here will be text-y, making keyboard events relevant. So…<\/p>\n

$(\"#payment-form input\").keyup(function() {\r\n  \r\n  \/\/ calculate progress\r\n\r\n});<\/code><\/pre>\n

4. Count Number of Valid Inputs<\/h3>\n

Whether or not any particular input is valid or not in its current state is part of the DOM now. Once we have a pointer to it, we just check this.validity.valid<\/code> for true or false.<\/p>\n

var numValid = 0;\r\n$(\"#payment-form input[required]\").each(function() {\r\n    if (this.validity.valid) {\r\n        numValid++;\r\n    }\r\n});<\/code><\/pre>\n

5. Adjust the Progress Bar \/ Display Messages<\/h3>\n

Now that we have an integer of how many inputs are valid, we just set up some basic logic to adjust the progress bar’s current value.<\/p>\n

This is where we could also display our motivational messages. Perhaps there is a <p><\/code> element somewhere on the page which we have a pointer to (progressMessage) and we simply adjust the text inside it to match the progress.<\/p>\n

\/\/ \"Cached\" somewhere once\r\nvar progress = $(\"#progress\"),\r\n    progressMessage = $(\"#progressMessage\");\r\n\r\n\/\/ Logic that runs after counting every time\r\nif (numValid == 0) {\r\n    progress.attr(\"value\", \"0\");\r\n    progressMessage.text(\"The form, it wants you.\");\r\n}\r\nif (numValid == 1) {\r\n    progress.attr(\"value\", \"20\");\r\n    progressMessage.text(\"There you go, great start!\");\r\n}<\/code><\/pre>\n

Browser Support<\/h3>\n

If browser support is a concern, you’ll probably want to run a little Modernizr test first. If it passes, then load scripts, inject elements, however you want to handle it.<\/p>\n

if (Modernizr.input.required) {\r\n\r\n  \/\/ Modernizr.load, inject elements, whatever.\r\n\r\n}<\/code><\/pre>\n

If you need more help on that, read this article<\/a>.<\/p>\n

Demo on CodePen<\/h3>\n
<\/code><\/pre>\n

Do whatever you’d like with it<\/a>. <\/p>\n","protected":false},"excerpt":{"rendered":"

Imagine you have a form with 10 required fields on it. That’s a bit larger than your average form on the internet and a good bit of work for a user. You might lose some users along the way, which is never a good thing. Perhaps to encourage finishing the form, you let them know […]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"sig_custom_text":"","sig_image_type":"featured-image","sig_custom_image":0,"sig_is_disabled":false,"inline_featured_image":false,"c2c_always_allow_admin_comments":false,"footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":[]},"categories":[4],"tags":[],"jetpack_publicize_connections":[],"acf":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"featured_media_src_url":null,"_links":{"self":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts\/19051"}],"collection":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/comments?post=19051"}],"version-history":[{"count":13,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts\/19051\/revisions"}],"predecessor-version":[{"id":19082,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts\/19051\/revisions\/19082"}],"wp:attachment":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/media?parent=19051"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/categories?post=19051"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/tags?post=19051"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}