There is more infrastructure and technology to an inputfield than you might expect at first glance.

In your application, admins shouldn't be able to create a user that has the same username or email of another user. You want to avoid duplicated entries. But when is the right time to display that your input was meaningless? You should get that information ahead of time. Way before you hit the big submit button, just to find out that the user already exists and you have to go search for them. The time you needed to tediously fill in all the fields for that user? Wasted!

Today we are going to build one ot these inputs, to ease filling in those forms.

What are we building?

An inputfield to do realtimechecking if a user already exists in the system

Finding out what data is required for this input field

Combining HTTP, Form, Ajax and Validation to get feedback before submission

Below, you can find a little example of how this could work in action.

What looks like no big deal, requires a lot of prep work. Open your devtools and inspect the network tab. Here you can click XHR to only listen to AJAX calls. When typing another username, you will see the results.

The devtools are your tool, to make sure that the requested data correctly gets to the front-end as expected. To see what that data looks like, click on Preview. I prepared a little REST API with fake data, so you can test for usernames and e-mail addresses. You can open that URL to test how the API works. Click the button below to see all users that start with "jo".

The REST API is made to deliver pure data. No HTML, no extra load. This makes loading additional data on the fly easier and also fast. In our case, we get a bunch of JSON data.

Becoming aware of your information architecture

Note: Before you start designing and developing that fancy input, you should be aware of the information architecture at your disposal.

This step is absolutely crucial!

Are you responsible for both front, and back-end but have little understanding of the underlying system? This sucks!

Do you have a back-end developer in your project you can turn to? Get in touch with them! Let them know, you want to asynchronously fetch data for a more interactive user interface. Let them know that you need a way to find out if that user already exists using a REST API. Most full-stack solutions come with their own way of setting this up. Confluence for instance, has a well documented API. Even Wordpress offers a REST API to retrieve a list of users. All those APIs have in common, that they deliver JSON data.

If for the sake of the guide, you are out of luck and there is no API at your hands, make sure to follow through, using the testing API I created for you.

Building the asynchronous input validation mechanism

To start, we need a simple form with an input. No magic here. Right now, you do not have to care about what the action of the form is, since we just have to take care about the input. However, it is always recommended to use a form to group together any inputs used on your website.

Let's evaluate our options for validating the input. We could use the input attribute pattern and create a regex list of all users that are not allowed to pick. However, this would require to place your entire userbase into one input field.

The performance-wise better option you have here, is to perform an AJAX call to the REST API, whenever a username is entered. Optionally, you can debounce the input to avoid flooding the server.

When the input matches a name in the database, we set a pattern attribute Validation is then controlled via the constraint validation API.

The only two methods you need for validation

There are two functions that make custom validation and real-time checking possible:

element.setCustomValidity('User already exists')

This will let you set custom error messages for inputfields. Setting this to an empty string, makes the input valid. But only if no other constraints, like the required attribute, mark the input as invalid. By the way, you can style invalid inputs with the :invalid pseudo selector.

That's it! You can combine your REST API with your forms. You can use your favorite HTTP client like axios, jquery or similar. However, by using the elegant async/await syntax together with fetch, you will only require just a few lines of code. Development comfort is important!

Do you need help with implementing this into your project?

Do you need inspiration how to solve those tricky issues? Issues that when resolved, would ease a lot of daily user interface interaction.

Get on my list! You are welcome to reply to any email with your important questions and get a hand-crafted reply with hands-on guides on how to solve your individual use-cases. Fill in your name below and get in touch today :) . It is about a nice conversation from developer to developer after all.