Min and Max length input validation in React

Today, we will show you how to implement min and max length input validation in React. In the previous article, we explained to you how to create a common input component and reuse it for validation in React.

Demo Application

Output - Min and Max length input validation in React - Clue Mediator
Output – Min and Max length input validation in React – Clue Mediator

Min and Max length input validation in React

  1. Create a common Input component
  2. Add Min and Max length validation in the Input component
  3. Use the Input component for validation
  4. Output

1. Create a common Input component

Before starting the example, I will suggest you to refer the previous article to create a common Input component.

Reference: Input validation in React

2. Add Min and Max length validation in the Input component

Let’s extend the functionality for min & max validation in the Input component. Here, we will slightly change the validationHandler function and add the min & max validation for the input field.

Your full component will look like this.

Input.js

3. Use the Input component for validation

Now, we will use the common Input component for validation. We will take a userId and ticketNumber to add the validation of the min & max length.

Let’s render an HTML code for these two fields where we will set a minimum 6 digit and maximum 8 digit validation for User Id and Ticket Number respectively.

Let’s put all things together and see how it looks.

App.js

4. Output

Run the application and check the output in the browser.

That’s it for today.
Thank you for reading. Happy Coding..!! 🙂

Demo & Source Code

GitHub Repository StackBlitz Project
If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *