输入文本验证反应原生

问题描述 投票:0回答:2

我有以下代码:

 <TextInput placeholder={"PID"}  keyboardType = 'numeric'  editable = {true}    maxLength = {10} value={this.state.text}  onChangeText={(text) => this.setState({text})} />   
 <Button title='NEXT' onPress= {() => navigate('service', { text: this.state.text})}> </Button>

我想像这样验证输入字段:如果输入的长度等于10,则按下按钮转到服务页面。否则显示警告消息。

我怎样才能做到这一点?

reactjs react-native react-native-android
2个回答
1
投票
manage = () => {
    if (this.state.text.length === 10) {
        navigate('service', { text: this.state.text})})
    } else {
        alert('Your PID must be exactly 10 characters!')
    }
}

render() {
    return (<Button onPress={this.manage} ... />)
}

0
投票

亲爱的:)您需要使用路由包,如react-navigation或react-native-router-flux(我喜欢后者),用它定义您的页面,并在onPress函数中使用简单的if else语句...

每个路由包都提供简单的编程导航调用。

© www.soinside.com 2019 - 2024. All rights reserved.