Add React Input Form Mask & Validation Features to Your Web App
Form elements are a common way to collect information that your website requires from users. They typically consist of <input> elements, checkboxes, radio buttons, and a button for users to submit the information they entered. However, standard <input> elements allow users to enter whatever they want, and if you want to standardize the information they entered, it's on the developers to implement that functionality.
That is where the InputMask comes in; Wijmo’s React InputMask control allows developers to implement masks, predefined formats for the input field, easily. This will enable you to restrict the number of characters that users can enter in the mask, the prompt characters displayed in the control, and whether the control allows users to enter numbers, letters, or both.
In this article, we’ll be outlining the following steps:
- Creating a React Form
- Implementing Wijmo’s React InputMask
- Defining the Mask and Prompt Characters
- Validating the User’s Information
If you’d like to download the project for yourself, you can find it on GitHub. Also, the code is available on StackBlitz.
Interested in what Wijmo has to offer? You can download the library here!
Creating a React Form
The first thing that we’ll go over is creating the React form. Open up the App.js file and add the following markup for the form:
<div class="form-control">
<div class="form-header">
<span>User Information</span>
</div>
<form class="form-body" onSubmit={submitForm}>
<div class="form-footer">
<button class="form-button" type="submit">Submit</button>
</div>
</form>
</div>
We’re using a form to aggregate the data from the user and React’s onSubmit markup to tie a method to the form to handle when the form gets submitted. Be sure to add this function above the markup so we don't get an error:
function submitForm(event) {}
The other thing that we’ll do in this section is add styles for the form. Open up the App.css file and add the following CSS:
.form-control {
position: absolute;
width: 400px;
height: 300px;
z-index: 15;
top: 50%;
left: 50%;
margin: -150px 0 0 -200px;
border-radius: 15px;
box-shadow: 1px 0 5px -2px rgb(90, 90, 90);
text-align: center;
}
.form-header {
height: 50px;
width: 100%;
line-height: 50px;
border-top-left-radius: 15px;
border-top-right-radius: 15px;
background: rgb(100, 100, 252);
font-weight: bold;
font-size: larger;
color: white;
}
.form-body {
height: 100%;
position: relative;
}
.form-footer {
position: absolute;
bottom: 75px;
right: 2px;
height: 50px;
width: 100%
}
.form-button {
float: right;
margin-top: 10px;
margin-right: 10px;
height: 40px;
width: 100px;
border-radius: 10px;
border: 1px solid black;
background: rgb(100, 100, 252);
color: white;
font-size: 16px;
}
.form-button:hover {
cursor: pointer;
background: rgb(140, 140, 255);
}
The CSS we’ve just written will center the form on the page and style our header, footer, and button elements. Now, if you run the application, you should see the following:
Implementing Wijmo’s React InputMask
Now that the form has been created, it's time to add our InputMasks. To use Wijmo, we will need to add the library to our project. Open up your command prompt, navigate to where your project is stored, and run the following:
npm i @grapecity/wijmo.react.all
Once NPM has installed all the files, open up the App.js file. Now that we’ve included Wijmo, we need to import the required Wijmo files to our product:
import * as wjcCore from '@grapecity/wijmo';
import * as wjcInput from '@grapecity/wijmo.react.input';
The last thing to do is add Wijmo’s CSS library to our application. Open up the index.css file and add the following:
@import '@grapecity/wijmo.styles/themes/wijmo.theme.material.css';
body {
box-sizing: border-box;
font-family: sans-serif;
}
With all of that done, we can now use Wijmo’s React InputMask. Go back to the App.js file and add the following markup inside of the <form> element:
<div className="form-element">
<wjcInput.InputMask ref={name} id="name" placeholder="Name"></wjcInput.InputMask>
</div>
<div className="form-element">
<wjcInput.InputMask ref={email} id="email" placeholder="Email"></wjcInput.InputMask>
</div>
<div className="form-element">
<wjcInput.InputMask ref={phone} id="phone" placeholder="Phone Number"></wjcInput.InputMask>
</div>
<div className="form-element">
<wjcInput.InputMask ref={social} id="social" placeholder="Social Security Number"></wjcInput.InputMask>
</div>
Here, we’re implementing four InputMasks: one for name, email, phone number, and social security number. We’re also setting a placeholder value for each mask, which will display inside the InputMask to let users know the expected input.
We also need to import createRef and add the following code to our App.js file:
import { createRef } from "react";
const name = createRef();
const email = createRef();
const phone = createRef();
const social = createRef();
The last thing that we’ll need to do is add some CSS to style the InputMasks in our App.css file:
.form-element {
text-align: center;
margin-top: 15px;
width: 100%;
}
Now, if you run the application, you should see the following:
Defining the Mask and Prompt Characters
Now that we’ve implemented the InputMasks, we’ll review how to set the mask and prompt characters for the controls. In this sample, we want to define a mask and prompt characters for the phone number and social security number inputs. We’re going to modify the markup for both of these InputMasks:
<wjcInput.InputMask ref={phone} id="phone" placeholder="Phone Number" mask="000-000-0000" isRequired={false} value="" promptChar="#"></wjcInput.InputMask>
<wjcInput.InputMask ref={social} id="social" placeholder="Social Security Number" mask="000-00-0000" isRequired={false} value="" promptChar="*"></wjcInput.InputMask>
By using number characters in the mask property, we’re telling the InputMask that we only want to accept numbers when typing in the control and how many characters we require the user to input. Setting the prompChar property tells the control what character we want to show for the values that we still need input for. And finally, setting the isRequired and value properties will allow our placeholder to continue to display instead of defaulting to displaying the prompt characters. Now, if we run the application and begin typing in either the phone or social InputMask, we should see the following:
As you can see, when we type in either of those InputMasks, it will display the prompt character for the remaining characters that the control expects.
Validating the User’s Information
The last thing we’ll discuss in this article is how we can validate the user’s information. In the case of this form, we want them to enter information in the “Name” and “Email” InputMasks, and we want to make sure that the “Phone Number” and “Social Security Number” InputMasks have been completely filled out.
We’ll first make sure that users know that the last two InputMasks need to be completely filled out. We’ll use Wijmo’s React InputMask valueChanged event to do that. Inside of the markup, add the event to the control:
<wjcInput.InputMask ref={phone} id="phone" placeholder="Phone Number" mask="000-000-0000" isRequired={false} value="" promptChar="#" valueChanged={valueChanged}></wjcInput.InputMask>
<wjcInput.InputMask ref={social} id="social" placeholder="Social Security Number" mask="000-00-0000" isRequired={false} value="" promptChar="*" valueChanged={valueChanged}></wjcInput.InputMask>
Now, inside the App.js file, we’ll implement the following method:
function valueChanged(ctrl) {
wjcCore.toggleClass(ctrl.hostElement, 'state-invalid', !ctrl.maskFull);
}
This method checks if the maskFull property of the InputMask returns true; maskFull is a Boolean value that returns true if the user has entered the required number of characters by the mask and false if not. If it does not return true, then we add a CSS class named state-invalid to the control.
We’ll add that CSS class to the App.css file:
.state-invalid {
color: red;
}
Now, as we type in the “Phone Number” or “Social Security Number” InputMasks, we’ll see the following:
As you can see, if the mask has not been filled out completely, the text will display as red.
Next, we’ll add a method to check and see if the user has filled out all of the InputMasks, as well as update the onSubmit() method to determine whether or not to submit the form:
const name = createRef();
const email = createRef();
const phone = createRef();
const social = createRef();
function isFormComplete() {
if(name.current.control.value !== '' && name.current.control.value !== '' && name.current.control.maskFull && name.current.control.maskFull) {
return true;
}
return false;
}
function submitForm(event) {
if(isFormComplete()) {
alert('User Information:\nName: ' + name.current.control.value +
'\nEmail: ' + email.current.control.value + '\nPhone Number: ' + phone.current.control.value +
'\nSSN: ' + social.current.control.value)
} else {
event.preventDefault();
}
}
Now, if we run the app and properly fill out all of the InputMasks, we’ll see the following alert:
Conclusion
As you can see, you can easily build forms using Wijmo’s React InputMask control. If you'd like more information, we've got demos, documentation, and API references for developers. If you'd like to download the finished application, you can find it on GitHub and on StackBlitz.
Interested in what Wijmo has to offer? You can download the library here!
Happy coding!