React Native TextInput that only accepts numeric characters

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

     this.onChanged(mobile)}/>
    

    and onChanged method :

    onChanged(text){
       var newText = '';
       var numbers = '0123456789';
       if(text.length < 1){
         this.setState({ mobile: '' });
       }
       for (var i=0; i < text.length; i++) {
            if(numbers.indexOf(text[i]) > -1 ) {
                 newText = newText + text[i];
            }
            this.setState({ mobile: newText });
        }
    }
    

提交回复
热议问题