Use Input Masks for Better UX and Easier Validation

Recently I was developing a few forms for a project at work and wanted a fairly specific format for the input of one of the fields. It was a time of day, and since the <time> element isn’t very well supported in browsers yet, I opted for my own text input and validation. While I do provide some examples for users and check the input on the server side, I opted for input masks on the front end of development to make the form validation easier, and more importantly, to make using the form easier.

You can see what I did, as well as an implementation on CodePen. It’s also embedded below. The description in the pen is this:

Here’s a little thing I did with data input masks for an app I’m building. It is using jQuery and the input mask plugin.The first one is exactly like the demo on the page. The one below uses attribute selectors and demonstrates the mask can be used on form element with the same name (for arrays and such).

In the original plugin documentation, it is suggested that IDs on the form elements are used; however, I was looking for a way to knock out a list of form elements that would be put into the same array (and therefore have the same name) without having to name them #id1, #id2, etc. Since jQuery breaks down the DOM extremely well, I was able to use input attribute selectors, similar to what you use in CSS, to have 1 line that will apply the input mask to all of the form elements.

The reason I used input masks, as stated before, is 2-fold. This was easier than parsing out the string in my validate function. Instead of breaking out the string (the same way I do in PHP already) to make sure the format is correctly, I use input masks to guide the user in the right direction. It also makes for a better User Experience; instead of users potentially entering the input incorrectly and then having to re-enter up to 10 different times of day, they know they are doing it right on the first shot.

The pen below is just a simple example of what input masks can do. The jQuery inout mask plugin comes with a bunch of different examples and common formats. I recommend you check it out!

[codepen_embed height=”600″ theme_id=”4667″ slug_hash=”sjGdq” default_tab=”result”]See the Pen <a href=’http://codepen.io/jcasabona/pen/sjGdq/’>Form Input Masks with Attribute Selectors</a> by Joe Casabona (<a href=’http://codepen.io/jcasabona’>@jcasabona</a>) on <a href=’http://codepen.io’>CodePen</a>.[/codepen_embed]

Similar Posts

  • GitGutter for Sublime Makes Diffs Easy

    At Crowd Favorite we use git for everything, which is a nice change from my old workflow of hoping for the best. I’ve learned quite a bit about git and Github since starting and have looked for tools to help me do things better. A really simple, but super helpful, tool is the GitGutter package for…

  • |

    Add IE Conditionals to wp_enqueue_style

    When creating a WordPress theme, it’s best practice to use wp_enqueue_style for adding all stylesheets, including style.css. At first glance, this can pose a challenge if you want to conditionally include CSS based on the browser (like IE-only styles, for example). Luckily, there is a quick way to do this in WordPress using $wp-styles: global…

  • Coding

    Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don’t hold it against me. One of the perils of having a 20+ year old website!This semester I am taking CompSci II, an advanced Java programming course at my school. So I figured I might as…

  • | | |

    Version 4.0

    Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don’t hold it against me. One of the perils of having a 20+ year old website!The New TehJoey is officially launched. You may have been noticing over the last few days, when layouts were switching back…

  • |

    What is a Hackathon?

    According to Wikipedia, a hackathon is defined as: …an event in which computer programmers and others involved in software development, including graphic designers, interface designers and project managers, collaborate intensively on software projects. Basically, it’s a place were people who work with computers can come together to hopefully make something awesome. There are all types…

  • Coding Projects as my Side Hustle

    I did some thinking over the weekend, after I wrote the post about my learning plan. I looked at my project pipeline. I looked at what people were hiring me to do, and I reviewed the next few personal projects I’m working on. You know what I saw? No coding projects. Honestly, I shouldn’t. That’s…