In React Native using <TextInput/>
, I am trying to make /
appear only when the <TextInput/>
is focused, and would stay there if another input is entered in. Currently, the format is MM/YY
, so when the user types the third digit, it would go after the /
, and if the user were to press back, it would delete the digit before the /
.
So what would be the right approach to implementing previously mentioned? Thank you and will be sure to accept the answer.
I tried the following but getting an error with length, and this is only adding /
after two digits have been entered:
_changeCardExpiry(value) {
if (value.indexOf('.') >= 0 || value.length > 5) {
return;
}
if (value.length === 2 && this.state.cardExpiry.length === 1) {
value += '/'
}
//then update state cardExpiry
}
...
<TextInput
onChangeText={this._changeCardExpiry.bind(this)}
placeholder='MM/YY'
value={cardExpiry}
/>
if (text.length === 2 && this.state.cardExpiry.length === 1)
sayingCannot read property 'length' of undefined.
– Doggone