How to validate mobile number length in ReactJS ?
Last Updated :
24 Jul, 2024
Validating mobile number length in React JS App is an important step to check whether the number entered by the user is genuine or not. It is effective in many case like creating a user form, collection of employee details, etc.
Approaches to validate mobile number length in React Js are
Creating React Application
Step 1: Create a React application using the following command:
npx create-react-app foldername
Step 2: After creating your project folder i.e. foldername, move to it using the following command:
cd foldername
Project Structure
It will look like the following.

Implement a regular expression for input length to validate the mobile number input. This regular expresstion will return false if the input length is more than 10 digits and true if length is valid.
Example: This example implements a form with mobile number input field validated using regex.
JavaScript
import React, { useState } from 'react';
const App = () => {
const [mobileNumber, setMobileNumber] = useState('');
const [message, setMessage] = useState('');
const handleInputChange = (event) => {
const value = event.target.value;
if (/^\d*$/.test(value)) {
setMobileNumber(value);
// Regular expression to match exactly 10 digits
const regex = /^\d{10}$/;
if (value.length === 0 || regex.test(value)) {
setMessage(value.length === 10 ? 'Mobile number is valid.' : '');
} else {
setMessage('Mobile number must be exactly 10 digits.');
}
} else {
setMessage('Only digits are allowed.');
}
};
return (
<div>
<h1>Mobile Number Validation</h1>
<input
type="text"
value={mobileNumber}
onChange={handleInputChange}
placeholder="Enter your mobile number"
/>
{message && (
<p style={{ color: message === 'Mobile number is valid.' ? 'green' : 'red' }}>
{message}
</p>
)}
</div>
);
};
export default App;
Steps to run the applicaition: Use this command to run the output
npm start
Output: This output will be visible on http://localhost:3000/ on browser window.

Approach 2: Using Material UI component
Material UI for React has this component available for us and it is very easy to integrate. We can use the error Property for TextField Component in ReactJS using the following approach.
Steps to Install MUI: Install the material-ui modules using the following command:
npm install @material-ui/core
Dependencies list after installtion
{
"dependencies": {
"@material-ui/core": "^4.12.4",
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}
}
Example: Implemented TextField input from Material UI with input length validation.
JavaScript
// Filename - App.js
import React, { useState } from "react";
import TextField from "@material-ui/core/TextField";
import InputAdornment from "@material-ui/core/InputAdornment";
const App = () => {
const [mobile, setmobile] = useState("");
const [isError, setIsError] = useState(false);
return (
<div
style={{
marginLeft: "40%",
}}
>
<h2>
Validate Mobile number length in ReactJS?
</h2>
<TextField
type="tel"
error={isError}
value={mobile}
label="Enter Phone Number"
onChange={(e) => {
setmobile(e.target.value);
if (e.target.value.length > 10) {
return setIsError(true);
}
setIsError(false);
}}
InputProps={{
startAdornment: (
<InputAdornment position="start">
+91
</InputAdornment>
),
}}
/>
<h3>
Your Mobile Number is:
{isError ? "Invalid" : "+91" + mobile}{" "}
</h3>
</div>
);
};
export default App;
Step to Run Application: Run the application using the following command from the root directory of the project.
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output. In the above example, we can see when the user exceeds the 10 digits, the TextField color turns to red to indicate the error, this is how we can validate mobile number length in ReactJS.
Similar Reads
How to Validate Octal number in ReactJS?
The Octal numeral system is the base-8 number system which uses the digits 0 to 7. It is also known as Oct for short. The following example shows how to validate the user entered data and check whether it is valid or not using the npm module in the ReactJS application. ApproachTo validate octal numb
2 min read
How to validate Passport Number is ReactJS?
Passport Number validation is an important step in order to authenticate the user's passport number. The following example shows how to validate the user's passport number using the npm module in ReactJS. Syntax: isPassportNumber(str, countryCode) Parameters: This function accepts two parameters as
2 min read
How to Validate an Email in ReactJS ?
Validating email in React is an important step to authenticate user email. It ensures the properly formatted email input from the user. The following example shows how to validate the user entered email and checking whether it is valid or not using the npm module in React Application. ApproachTo val
2 min read
How to validate URL in ReactJS?
URL (Uniform Resource Locator) is a reference/address to a resource on the Internet. For example, www.geeksforgeeks.com is a URL. The following example shows how to validate the user entered data and check whether it is valid or not using the npm module in the ReactJS application. Creating React App
2 min read
How to Implement Form Validation in React Native ?
React Native is a JavaScript framework for cross-platform mobile app development. Expo CLI simplifies React Native development with a streamlined process and helpful tools. In this article, we'll see how to implement form validation in react native. Form validation ensures the validity of user input
4 min read
How to Validate a Date in ReactJS?
Validating input Date in react ensures the correct date input. The valid date input is commonly used in case of calculating days, getting DOB for user data, and other operations etc. Prerequisites:React JSNode JS and NPMApproachTo validate a date in React we will use the validator npm package. Take
2 min read
How to Validate Number String in JavaScript ?
Validating a number string in JavaScript involves ensuring that a given string represents a valid number. This typically includes checking for digits, optional signs (+/-), decimal points, and possibly exponent notation (e.g., "1.23e4"). We will use various methods to validate number strings in Java
2 min read
How to apply validation on Props in ReactJS ?
Need of Validating Props in React JS Props are used to pass the read-only attributes to React components. For the proper functioning of components and to avoid future bugs and glitches it is necessary that props are passed correctly. Hence, it is required to use props validation to improve the react
3 min read
How to perform form validation in React?
Form validation in React involves ensuring that the data entered into a form meets certain criteria before submission. In this, we will see the form validation in React. Pre-requisitesNodeJS and NPMReactJSReact useState hookHTML, CSS, and JavaScriptSteps to Create React Application And Installing Mo
4 min read
Credit Card Number Validator using ReactJS
Credit card validation is an important step in every application in order to authenticate the user's credit card number so that the application can proceed to the payment process if needed. It can be achieved using the validator module in ReactJS. The following example shows how to validate the user
2 min read