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
How to Add Phone Number Input in React Native ? React Native is a JavaScript framework for cross-platform mobile app development. In this article, we'll see how to add a phone number input field in a React Native app using Expo CLI.âAdding a phone number input field in React Native is helpful for collecting user phone numbers during registration
3 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 Generate Random Numbers in React Native ? Generating random numbers is a fundamental aspect of React Native development. It enables various functionalities like generating game elements, creating unique identifiers, and implementing randomized UI components. In this article, we are going to see how we can generate a random number by using R
7 min read
How to Convert Input Value in Md5 using React Native ? In this article, we'll explore how to convert an input value to MD5 using React Native. Hashing, a fundamental teÂchnique in computer scienceÂ, involves converting data of any size into a fixeÂd-size value for security or inteÂgrity purposes. One widely useÂd hashing algorithm is MD5 (Message Digest
3 min read
How to Create a Text input with helper text in react-native? React native is a framework developed by Facebook for creating native-style apps for iOS & Android under one common language, JavaScript. Initially, Facebook only developed React Native to support iOS. However, with its recent support of the Android operating system, the library can now render m
2 min read