Skip to content
the poor coder

react native hidden text on textinput

Poor Man
Published
Reading time
2 min read
On this page

How to hide text on TextInput using React Native?#

React Native is a popular framework used for creating mobile applications. Often, we need to hide the text input from the users, especially while entering sensitive data such as passwords. In this PAA, we will discuss how to hide text on TextInput using React Native.

Method 1: Using 'secureTextEntry' property#

The easiest method to hide text on TextInput is by using the 'secureTextEntry' property. When this property is set to 'true', the entered text will be masked, and the user will not be able to see what they are typing.

Text

<TextInput secureTextEntry={true} />

The above code will create a TextInput where the entered text will be masked. You can also set the 'secureTextEntry' property dynamically based on certain conditions.

Method 2: Using 'passwordRules' property (iOS only)#

In iOS 12 and later, there is a new property called 'passwordRules', which allows us to customize the password policy for a TextInput. We can use this property to mask the entered text.

Text

<TextInput passwordRules={ {allowPassword: false} } />

The above code will create a TextInput where the entered text will be masked. We can also customize the password policy based on our requirements.

Method 3: Using 'onKeyPress' event#

If you want to show some characters while typing and mask the rest, you can use the 'onKeyPress' event. In this method, we will listen to every key press and update the state with the entered text.

Text

import React, {useState} from 'react';
import {TextInput} from 'react-native';

const HiddenTextInput = () => {
  const [text, setText] = useState('');
  const onChangeText = (inputText) => {
    let hiddenText = '';
    for (let i = 0; i < inputText.length; i++) {
      hiddenText += '•'; // replace with your desired character
    }
    setText(hiddenText);
  };
  return (
    <TextInput value={text} onChangeText={onChangeText} />
  );
};

The above code will create a TextInput where the entered text will be masked with the '•' character. You can replace it with your desired character.

Conclusion:#

Hiding text on TextInput in React Native is simple and can be achieved using various methods. You can choose the method based on your requirements and preferences.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC