How to restrict user character input limit in ReactJS ? Last Updated : 09 Nov, 2023 Comments Improve Suggest changes Like Article Like Report To restrict the user character input limit in React JS we can simply set a max input limit to the input box or we can also monitor the input length and make a custom function to set the restriction. Pre-requisite:NPM & Node.jsReact.jsReact HooksHTML onchange EventApproaches to restrict user character input limit in React JS are: Table of Content Using maxLength attributeUsing onChange eventSteps to Create React Application:Step 1: Go to your command prompt and write the below command to create a react app. npx create-react-app <YOUR_APP_NAME>Step 2: Then go to your app folder by typing the below command cd <YOUR_APP_NAME>Project Structure: Approach 1: Using maxLengthWe will use maxLength attribute for our input. It is the same as the maxlength attribute used for HTML. It restricts the user to enter characters till it reaches the maxLength limit that we have set. Example: This example uses max legth attribute to implement the restriction for character input. JavaScript // Filename - App.js function App() { return ( <div className="App"> <label>Name:</label> <input name="name" type="text" maxLength={10} /> </div> ); } export default App; Step to run the application: Run the following command to start your app in your localhost:3000. npm startOutput: This output will be visible on the http:// localhost:3000 on the browser window. We can see that after the 10th character we cannot further add anything. Approach 2: using onChange eventThis approach include creating a simple form component that asks the user to enter their username, the moment the character length exceeds more than 10 an alert will be shown. In this file, we are using useState a react-hook to maintain the state userName. We will create a function handleChange that will track the length of the user of the character is putting, here 'e' is the browser event. If it is equal to 10 then the alert will pop up or else it will be added to the state. Example: This example implements character limit for the user input using the onChange event. JavaScript // Filename - App.js import React, { useState } from "react"; const Form = () => { const [userName, setuserName] = useState(""); const handleChange = (e) => { // Here we are checking if the length is equal to 10 if (e.target.value.length === 10) { window.alert( "Username shouldn't exceed 10 characters" ); } setuserName(e.target.value); }; return ( <div> <label>Please enter username:</label> <input name="username" value={userName} onChange={handleChange} /> </div> ); }; function App() { return ( <div className="App"> <h1>Hey! Geek Welcome</h1> <Form /> </div> ); } export default App; Step to run the application: Run the following command to start your app in your localhost:3000. npm startOutput: This output will be visible on the http:// localhost:3000 on the browser window. Comment More infoAdvertise with us Next Article How to restrict user character input limit in ReactJS ? aniluom Follow Improve Article Tags : Web Technologies ReactJS React-Questions Similar Reads How to Set Character Limits in HTML Input Fields? It's often necessary to restrict the number of characters a user can input. This ensures data consistency and prevents users from entering values that are too long or too short. In this article, we will learn how to set the minimum and maximum number of characters allowed in an HTML input field.To s 2 min read How to create a translucent text input in ReactJS ? We are going to learn how to create a translucent text input in React JS. We are going to create a translucent animated text input using framer-motion and styled components.Prerequisites:JavaScript (ES6)React JSReact useStatestyled-componentsframer-motion Approach: We are going to create a transluce 4 min read How to create basic text input in React Native ? Text input is a basic component that allows users to input text through the keyboard. It is widely used in mobile applications for taking usernames, passwords, user details, and much more.In React Native, we have a TextInput component to achieve this. It comes with many props that help us to modify 4 min read How to Trim White Spaces from input in ReactJS? Trim white spaces from input removes the spaces from the start and end of the input string in react. The article covers how to remove whitespace from input in react.ApproachTo remove or trim the white spaces from the input in react we will use the validator npm package. Install the validator package 2 min read How to add code input in React JS? In this article, we are going to learn how we can add Code Input in React JS. React is a front-end JavaScript library for constructing user interfaces or UI components. It is maintained by Facebook and a community of individual developers and companies. Approach to add code input: To incorporate our 2 min read How to create OTP Input Box Using React ? Let us explore the implementation of OTP (One-Time Password) login functionality using ReactJS. OTP-based logins add an extra layer of security to your applications, making them more resilient against unauthorized access.Prerequisites:ReactHTML, CSS and JavaScriptApproach:Create a React functional c 3 min read How to add AutoSize Input in React.js ? We are going to learn how we can add AutoSize input in ReactJs. React is a free and open-source front-end JavaScript library for building user interfaces or UI components. Prerequisites:Nodejs and NPMReact JSApproach:  To add our autosize input we are going to use the react-input-autosize package. T 2 min read How to debounce or throttle input changes in React? Debouncing and throttling are techniques used to limit the frequency of certain actions, particularly useful for handling input changes like those from user interactions. Debouncing:Imagine you have a text input field where users are typing. Each keystroke triggers some action, like filtering search 3 min read How to set input box to be a floating number in ReactJS ? When working with forms in ReactJS, you may encounter scenarios where you need to accept floating-point numbers as input from users. We will set an input box to accept floating numbers in ReactJS and handle the validation of the entered values. PrerequisitesReact JS NPM and Node.jsApproach If we wan 2 min read How to Create Smaller `Input` in React-Bootstrap ? ReactJS is one of the most favorite libraries for building attractive applications. Combining Bootstrap with ReactJS is a powerful tool for making the application more interactive. A Smaller Input in React-Bootstrap refers to reducing the size of an input field component, typically achieved by apply 4 min read Like