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

  • | |

    Mobile Matters! Google to change Search Rankings

    If you’ve done a Google Search on your mobile device recently, you may have noticed that Google adds a bit of text in the search results alerting you to the fact that a website is (or is not) mobile friendly. This can have a big impact on your website’s search ranking.

  • Serve Your Customers to Help Yourself

    I’m a big fan of automations. Generally, they are “set it and forget,” and I don’t have to worry about them working. When they break, I spend a fraction of the time troubleshooting that I would performing the task manually. A great, easy automation for anyone is post new content to Twitter. And while there…

  • | |

    Creating a Responsive Gutenberg Price Table

    Last week I worked on an upcoming tutorial for a popular online publication on how to style the Gutenberg Columns block (I’ll be sure to send that along when it comes out). As as result, I decided to experiment to see what you could reasonable do, and came up with this Gutenberg Price Table: https://codepen.io/jcasabona/pen/RYvEYd. In…

  • |

    Quick Tip: Add a Widget to WordPress

    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!A couple of weeks ago I was working on a WordPress site and thought it might be nice to add a…

  • |

    5 Things Every Business Website should show on the Homepage

    You have definitely been here before. You’re on a website for a restaurant or store you perhaps what to visit. You look for some information that will help you, but all you see is some blurb about the business, maybe a slider of images, and other miscellaneous information. But that’s not what most people need…

  • |

    Make wp-caption the same (responsive) width as the Image

    I was working on a problem last week in WordPress where the caption for an image was extending the whole width of the container, not staying the width of the image. This make sense – the image is inside the .wp-caption container. So how to we fix it? Let me tell you!