Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to limit the length of a TextInput in React-Native?

Tags:

react-native

I've been trying to make it so that my program does not allow you to input more than a certain amount of characters into a TextInput component, similar to how maxLength works for input. I haven't been able to find anything similar maxLength for textInput. Is there an easy way to set a maximum number of characters for the < TextInput /> component?

like image 798
leonlxli Avatar asked Jul 07 '15 23:07

leonlxli


People also ask

How do I change the height of TextInput in react native?

Firstly Let's import React Native default TextInput to our code. Inside our component state we will define two properties value & height. value to hold our TextInput value & height to control our TextInput height.

How do you set maximum number of lines in TextInput in react native?

numberOfLinesSets the number of lines for a TextInput . Use it with multiline set to true to be able to fill the lines.

How do you limit the number of characters in an input field in React?

Approach 1: Using maxLength: We will use maxLength attribute for our input. It is the same as the maxlength attribute used for HTML. It restricts the user to enter characters till it reaches the maxLength limit that we have set.


1 Answers

In the props for TextInput you can set a maxLength attribute.

From the documentation:

maxLength number

Limits the maximum number of characters that can be entered. Use this instead of implementing the logic in JS to avoid flicker. @platform ios

like image 168
Andrew Ives Avatar answered Sep 20 '22 01:09

Andrew Ives