72

How do you set such attribute? For example the CCV field of credit card should have a max length of 3. Expiry date should have a max length of 2 for month and 4 for years.

puppymaster
  • 781
  • 1
  • 7
  • 7

3 Answers3

140

The maxLength prop is now part of React Native:

<TextInput value={this.state.text} maxLength={4} />
Dan Leveille
  • 3,001
  • 2
  • 24
  • 28
  • 3
    I have add maxLength={4} as the props of TextInput, but the simulator allows the input with length more than 4. Is this the limitation of simulator or I miss something? – 吳強福 Apr 15 '17 at 06:41
  • 2
    @吳強福 That's strange. It works fine for me. And you're on the latest version? (Though this was added well over a year ago...) – Dan Leveille Apr 17 '17 at 17:56
  • Is there any solution for 'Text' field? because 'maxLength' is not working for 'Text' field. – Luvnish Monga Jul 11 '18 at 07:16
  • 2
    maxLength' did not worked for 'TextInput' I had to add multiline={false} as well. Without adding this maxLength={4} did not worked – surhidamatya Aug 17 '18 at 06:40
9
<TextInput value={this.state.text} maxLength={1000} />

ot is working for me to set the input text length.

M Mahmud Hasan
  • 1,303
  • 1
  • 13
  • 20
3

Please make sure you are using maxLength with camel case and not maxlengh

it seems obvious but it wasn't to me when i was breaking my head trying to figure it out hope this helps

prog
  • 173
  • 1
  • 3
  • 12