202

I have a TextInput. Instead of showing the actual text entered, when the user enters text I want it to show the password dots / asterisks (****) you typically see in apps when typing a password. How can I do this?

<TextInput
  style={{ height: 40, borderColor: 'gray', borderWidth: 1 }}
  onChangeText={(text) => this.setState({input: text})}
/>
Penny Liu
  • 15,447
  • 5
  • 79
  • 98
bwbrowning
  • 6,200
  • 7
  • 31
  • 36
  • I am using 0.56RC secureTextEntry={true} Along with password={true} then only its working as mentioned by @NicholasByDesign – Ramusesan Jun 28 '18 at 05:12

15 Answers15

541

When this was asked there wasn't a way to do it natively, however this will be added on the next sync according to this pull request. Here is the last comment on the pull request - "Landed internally, will be out on the next sync"

When it is added you will be able to do something like this

<TextInput secureTextEntry={true} style={styles.default} value="abc" />

refs

Riley Bracken
  • 5,959
  • 2
  • 16
  • 17
  • 1
    thanks, so until that gets merged, what other options are there? I'm guessing facebook does something similar for logins to their own apps. – bwbrowning Mar 30 '15 at 02:33
  • 2
    I was looking into it today, that is how I found that pull request. They say they only have 2 apps that are 100% React Native. The F8 app it opens a new window asking for authorization. Facebook Ads has the functionality that we are looking for, but I almost think they wrapped Objective-C for it. Another way to do it would be to store the string and every time the input updates replace the last character with the ... stuff :). – Riley Bracken Mar 30 '15 at 02:46
  • 1
    @bwbrowning, it should be merged soon; long before you deploy I'd wager. – Brigand Mar 30 '15 at 03:24
  • `keyboardType={'phone-pad'}` if this is set then also it will not work. – Hardik Vaghani Nov 18 '21 at 04:28
51

May 2018 react-native version 0.55.2

This works fine:

secureTextEntry={true}

And this does not work anymore:

password={true} 
mediaguru
  • 1,807
  • 18
  • 24
26

Just add the line below to the <TextInput>

secureTextEntry={true}
ivcubr
  • 1,988
  • 9
  • 20
  • 28
Curious96
  • 392
  • 3
  • 7
15

Add

secureTextEntry={true}

or just

secureTextEntry 

property in your TextInput.

Working Example:

<TextInput style={styles.input}
           placeholder="Password"
           placeholderTextColor="#9a73ef"
           returnKeyType='go'
           secureTextEntry
           autoCorrect={false}
/>
Rohan K
  • 872
  • 11
  • 18
7

I had to add:

secureTextEntry={true}

Along with

password={true}

As of 0.55

NicholasByDesign
  • 781
  • 1
  • 11
  • 33
7

You need to set a secureTextEntry prop to true and for better user experience you can use an eye icon to show the actual password

 import {TextInput, Card} from 'react-native-paper';
 const [hidePass, setHidePass] = useState(true);

 const [password, setPassword] = useState('');


<TextInput
            
            label="Password"
            outlineColor="black"
            activeOutlineColor="#326A81"
            autoCapitalize="none"
            returnKeyType="next"
            mode="outlined"
            secureTextEntry={hidePass ? true : false}
            selectionColor="#326A81"
            blurOnSubmit={false}
            onChangeText={password => updateState({password})}
            right={
              <TextInput.Icon
                name="eye"
                onPress={() => setHidePass(!hidePass)}
              />
            }
          />
ABDULLAH
  • 550
  • 3
  • 13
6

An TextInput must include secureTextEntry={true}, note that the docs of React state that you must not use multiline={true} at the same time, as that combination is not supported.

You can also set textContentType={'password'} to allow the field to retrieve credentials from the keychain stored on your mobile, an alternative way to enter credentials if you got biometric input on your mobile to quickly insert credentials. Such as FaceId on iPhone X or fingerprint touch input on other iPhone models and Android.

 <TextInput value={this.state.password} textContentType={'password'} multiline={false} secureTextEntry={true} onChangeText={(text) => { this._savePassword(text); this.setState({ password: text }); }} style={styles.input} placeholder='Github password' />
Tore Aurstad
  • 3,189
  • 1
  • 27
  • 22
6

A little plus:

version = RN 0.57.7

secureTextEntry={true}

does not work when the keyboardType was "phone-pad" or "email-address"

Employee
  • 3,109
  • 5
  • 31
  • 50
qloveshmily
  • 1,017
  • 9
  • 5
4

If you added secureTextEntry={true} but did not work then check the multiline={true} prop, because if it is true, secureTextEntry does not work.

Mahmut Gundogdu
  • 543
  • 4
  • 12
4
<TextInput
  placeholderTextColor={colors.Greydark}
  placeholder={'Enter Password'}
  secureTextEntry={true} />
Antier Solutions
  • 1,326
  • 7
  • 10
4

 <TextInput
        placeholder="Password"
        secureTextEntry={true}
        style={styles.input}
        onChangeText={setPassword}
        value={password}
      />
3

You can get the example and sample code at the official site, as following:

<TextInput password={true} style={styles.default} value="abc" />

Reference: http://facebook.github.io/react-native/docs/textinput.html

Richard Li
  • 120
  • 2
  • 3
3

You need to set a secureTextEntry prop to true

<TextInput
  placeholder="Re-enter password"
  style={styles.input}
  secureTextEntry={true}
  value={confirmPsw}
  onChangeText={setconfirmPsw}
/>
Saeed Zhiany
  • 2,051
  • 9
  • 30
  • 41
1
<TextInput 
   secureTextEntry 
   placeholder="password" placeholderTextColor="#297542" 
   autoCorrect={false} style={mystyles.inputStylee} 
/>

You can simply add the secureTextEntry prop to TextInput and omit its value. By default, it is set to true. To make it to false do this secureTextEntry={false}

nima
  • 7,796
  • 12
  • 36
  • 53
chandru
  • 129
  • 9
  • 1
    While perfectly correct, I'd recommend setting it explicitely to {true} so one does not have to rely on remembering what's the default for that property – Sebastien F. Oct 03 '21 at 12:51
0
const [password, setPassword] = useState('');  
const [passwordVisibility, setPasswordVisibility] = useState(true);  
const [rightIcon, setRightIcon] = useState('eye');  
const [rightIconColor, setRightIconColor] = useState('#0C8A7B');

 const handlePasswordVisibility = () => {  
        if (rightIcon === 'eye') {  
            setRightIcon('eye-slash');  
            //setRightIconColor('#FF0000')  
            setPasswordVisibility(!passwordVisibility);  
        } else if (rightIcon === 'eye-slash') {  
            setRightIcon('eye');  
            //setRightIconColor('#0C8A7B')
            setPasswordVisibility(!passwordVisibility);  
        }  
    };  
 <TextInput  
     style={{
        height: 45,  
        margin: 12,  
        padding: 10,  
        backgroundColor: 'white',  
        borderRadius: 10  
    }}  
   name="password"  
   placeholder="Enter password"  
   autoCapitalize="none"  
   autoCorrect={false}  
   secureTextEntry={passwordVisibility}  
   value={password}  
   enablesReturnKeyAutomatically  
   onChangeText={text => setPassword(text)}  
  />

  <TouchableOpacity
      onPress={handlePasswordVisibility}>
  <Icon name={rightIcon} size={25} color={rightIconColor} />
  </TouchableOpacity>
</View>


**I Hope this will help you**
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 20 '22 at 03:08