React Native TextInput that only accepts numeric characters

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

    For Decimal /Floating point number only try this

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

    }

提交回复
热议问题