How to Get Only Numeric Values in TextInput Field in React Native ?
Last Updated :
11 Jan, 2025
Text input fields play a vital role in React Native applications, facilitating user interactions. However, certain situations demand restricting input to numeric values exclusively. In this article, we will delve into two distinct approaches to achieving this desired functionality.
Prerequisites
- Introduction to React Native
- React Native Components
- React Hooks
- Expo CLI
- Node.js and npm (Node Package Manager)
Steps to Create a React Native Application
Step 1: Create a react native application by using this command
npx create-expo-app <<Project-Name>>
Step 2: After creating your project folder, i.e. "Project-Name", use the following command to navigate to it:
cd <<Project-Name>>
Project Structure
.png)
Approaches to Accepts Number Only Input in React Native
- Using Regular Expressions
- Using isNaN Check
To restrict TextInput
to numeric values only, React Native provides properties like keyboardType="numeric"
and validation checks to ensure users input numbers.
Approach 1: Using Regular Expressions
In this approach, regular expressions are utilized to filter out non-numeric characters from the input text. The handleChange function applies a regular expression (/[^0-9]/g) to eliminate any characters that fall outside the numeric range. This guarantees that only numbers remain, thereby enhancing validation of user input. Additionally, by specifying the keyboardType prop as "numeric", mobile devices display the numeric keyboard, further enhancing the user experience.
Example:
JavaScript
import React, { useState } from "react";
import {
View,
TextInput,
StyleSheet,
Text,
} from "react-native";
const App = () => {
const [inputValue, setInputValue] = useState("");
const handleChange = (text) => {
// Allow only numbers
const numericValue = text.replace(/[^0-9]/g, "");
setInputValue(numericValue);
};
return (
<View style={styles.container}>
<Text style={styles.title}>Geeksforgeeks</Text>
<TextInput
style={styles.input}
onChangeText={handleChange}
value={inputValue}
keyboardType="numeric"
placeholder="Enter numbers only"
placeholderTextColor="#999"
/>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
alignItems: "center",
backgroundColor: "#f0f0f0",
},
title: {
fontSize: 24,
marginBottom: 20,
color: "green",
fontWeight: "bold",
},
input: {
width: 250,
height: 50,
borderWidth: 2,
borderColor: "#3498db",
borderRadius: 10,
paddingVertical: 10,
paddingHorizontal: 20,
fontSize: 18,
color: "#333",
backgroundColor: "#fff",
},
});
export default App;
Steps to Run:
To run react native application use the following command:
npx expo start
npx react-native run-android
npx react-native run-ios
Output:

Approach 2: Using isNaN Check
In this approach we are using the isNaN function to restrict input in React Native applications. When users input text, the function verifies if it is a valid number. If it fails this check, the input is disregarded, effectively preventing non-numeric entries. By employing this method, only numerical values are accepted, ultimately improving data accuracy and enhancing usability.
Example:
JavaScript
import React, { useState } from "react";
import {
TextInput,
View,
StyleSheet,
Text,
} from "react-native";
const App = () => {
const [number, setNumber] = useState("");
const handleNumberChange = (text) => {
if (!isNaN(text)) {
setNumber(text);
}
};
return (
<View style={styles.container}>
<Text style={styles.title}>Geeksforgeeks</Text>
<TextInput
style={styles.input}
value={number}
onChangeText={handleNumberChange}
keyboardType="numeric"
placeholder="Enter a number"
/>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
alignItems: "center",
backgroundColor: "#f0f0f0",
},
title: {
fontSize: 24,
marginBottom: 20,
fontWeight: "bold",
color: "green",
},
input: {
width: 250,
height: 50,
borderWidth: 2,
borderColor: "#3498db",
borderRadius: 10,
paddingVertical: 10,
paddingHorizontal: 20,
fontSize: 18,
color: "#333",
backgroundColor: "#fff",
},
});
export default App;
Output:
Similar Reads
React Native Tutorial React Native is a framework developed by Facebook for creating native-style applications for Android & iOS under one common language, i.e. JavaScript. Initially, Facebook only developed React Native to support iOS. However, with its recent support of the Android operating system, the library can
5 min read
Introduction to React Native If you want to build mobile apps for both Android and iOS. What should you learn? The individual native languages for each app i.e, Java for Android and Swift/Objective-C for iOS?, Actually NO. Native Android and iOS development are quite different and can be expensive â first, the language itself i
3 min read
What are the steps to create first React Native App ? React Native is an open-source UI software framework created by Meta Platforms, Inc. It is used to develop applications for Android, Android TV, iOS, etc. Weâre always looking for shorter development cycles, quicker time to deployment, and better app performance. And there are so many hybrid mobile
4 min read
Axios in React Native Axios is a widely used HTTP client for making REST API calls. You can use this in React Native to get data from any REST API.Axios in React NativeAxios is a library that helps you send HTTP requests in React Native apps. It allows mobile devices to communicate with a server, enabling them to send an
8 min read
Top React Native Apps to Build in 2025 Top React Native Apps to Build in 2025 is a popular framework for building high-performance mobile apps using JavaScript and React. It allows developers to create apps for both iOS and Android with a single codebase, saving time and resources. React Native, developed by Facebook. Initially, for iOS,
9 min read
How React Native works React Native is a popular framework for building mobile applications using JavaScript and React. It allows developers to write code once in JavaScript and run it on both Android and iOS devices, bridging the gap between web and native mobile development. In this article, weâll explore the main compo
5 min read
React Native FlatList Component FlatList is a React Native component that is a scrolling list that shows changing information while keeping the same look. It's great for long lists where the number of items can change. Instead of loading all items simultaneously, this component only shows what you can see on the screen. This makes
4 min read
What is a bridge in React Native ? A React Native app comprises two sides as given below.The JavaScript SideThe Native SideThe Native Side should be Java or Kotlin for Android and Swift or Objective-C for iOS.The huge reason for the popularity of React Native is that a bridge can be created between the JavaScript code and Native lang
7 min read
How React Native is different from ReactJS ? In this article, we will learn about how React Native is different from ReactJS. Both are popular JavaScript libraries. ReactJS is primarily used for building user interfaces on the web, while React Native extends its capabilities to mobile app development.React JSIt is a JavaScript library that sup
4 min read
What react-native :app:installDebug FAILED ? In this article we are going to fix this error i.e. if you are a react native developer then it's a big possibility that you might come across this error. Let's understand a solution that works wonders for this error. When does this error occur?This error occurs when we try to run our react-native p
2 min read