Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to make a React Native TextInput accept ONLY NUMBERS and make it accept numbers 1 to 10 ONLY?

How to make a React Native TextInput accept only numbers and make it accept numbers 1 to 10 only?

For example, say I input 11, the value should automatically change into the maximum allowed, that is, 10.

And when I input 0, the value should automatically change into the minimum allowed, that is, 1.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

As of now the only thing I have done are:

<TextInput
style={styles.input}
placeholder="Maximum of 10 passengers only"
maxLength={2}
keyboardType={"numeric"}
/>

>Solution :

You can use this:

...
const [value, setValue] = React.useState(0);
....

  React.useEffect(() => {
    if(value === "") {
      return;
    }
    if(+value > 10) {
      setValue(10)
    } else if (value < 1) {
      setValue(1);
    }

  },[value])


<TextInput
      keyboardType='numeric' 
      maxLength={2}
      value={value.toString()} 
      onChangeText={(e) => {
      setValue(e)
    }} />
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading