React Native TextInput that only accepts numeric characters

前端 未结 19 1140
轮回少年
轮回少年 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:19

    First Solution

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

      
            Enter Number
             this.onTextChanged(value)}
              value={this.state.number}
            />
          
    

    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

提交回复
热议问题