React Native TextInput that only accepts numeric characters

前端 未结 19 1020
轮回少年
轮回少年 2020-12-02 10:38

I need to have a React Native TextInput component that will only allow numeric characters (0 - 9) to be entered. I can set the keyboardType to

相关标签:
19条回答
  • 2020-12-02 11:17

    You can do it like this. It will only accept numeric values, and limit to 10 numbers as your wish.

    <TextInput 
       style={styles.textInput}
       keyboardType='numeric'
       onChangeText={(text)=> this.onChanged(text)}
       value={this.state.myNumber}
       maxLength={10}  //setting limit of input
    />
    

    You can see the entered value by writing the following code in your page:

    {this.state.myNumber}
    

    In the onChanged() function the code look like this:

    onChanged(text){
        let newText = '';
        let numbers = '0123456789';
    
        for (var i=0; i < text.length; i++) {
            if(numbers.indexOf(text[i]) > -1 ) {
                newText = newText + text[i];
            }
            else {
                // your call back function
                alert("please enter numbers only");
            }
        }
        this.setState({ myNumber: newText });
    }
    

    I hope this is helpful to others.

    0 讨论(0)
  • 2020-12-02 11:19

    First Solution

    You can use keyboardType = 'numeric' for numeric keyboard.

      <View style={styles.container}>
            <Text style={styles.textStyle}>Enter Number</Text>
            <TextInput
              placeholder={'Enter number here'}
              style={styles.paragraph}
              keyboardType="numeric"
              onChangeText={value => this.onTextChanged(value)}
              value={this.state.number}
            />
          </View>
    

    In first case punctuation marks are included ex:- . and -

    Second Solution

    Use regular expression to remove punctuation marks.

     onTextChanged(value) {
        // code to remove non-numeric characters from text
        this.setState({ number: value.replace(/[- #*;,.<>\{\}\[\]\\\/]/gi, '') });
      }
    

    Please check snack link

    https://snack.expo.io/@vishal7008/numeric-keyboard

    0 讨论(0)
  • 2020-12-02 11:19

    I had the same problem in iOS, using the onChangeText event to update the value of the text typed by the user I was not being able to update the value of the TextInput, so the user would still see the non numeric characters that he typed.

    This was because, when a non numeric character was pressed the state would not change since this.setState would be using the same number (the number that remained after removing the non numeric characters) and then the TextInput would not re render.

    The only way I found to solve this was to use the keyPress event which happens before the onChangeText event, and in it, use setState to change the value of the state to another, completely different, forcing the re render when the onChangeText event was called. Not very happy with this but it worked.

    0 讨论(0)
  • 2020-12-02 11:20

    I've created a component that solves this problem:

    https://github.com/amirfl/react-native-num-textinput

    0 讨论(0)
  • 2020-12-02 11:24

    A kind reminder to those who encountered the problem that "onChangeText" cannot change the TextInput value as expected on iOS: that is actually a bug in ReactNative and had been fixed in version 0.57.1. Refer to: https://github.com/facebook/react-native/issues/18874

    0 讨论(0)
  • 2020-12-02 11:24

    Here is my other simple answer to accept only numbers in the text box using Regular Expressions.

    onChanged(text){
        this.setState({ 
             myNumber: text.replace(/[^0-9]/g, '') 
        });
    }
    
    0 讨论(0)
提交回复
热议问题