javascript - 禁用 touchableopacity 直到验证为真
问题描述
我正在尝试禁用我的提交按钮,直到电子邮件和密码与我的验证参数相匹配,我可以为一个输入字段执行此操作,但是,我似乎无法围绕能够为多个文本输入执行相同操作,一切我试过没有工作,因为我什至尝试在 componentDidMount 中这样做。下面是我目前拥有的粗略示例代码。
class Login extends Component {
constructor(props) {
super(props);
this.state = {
showpass: true,
email: '',
password: '',
validEmail: true,
validPassword: true,
disabled: true,
};
}
onChangeEmail(email) {
const emailCheckRegex = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
this.setState({ email });
if (emailCheckRegex.test(email)) {
console.log('Email is Correct');
this.setState({
validEmail: true, disabled: false,
});
} else if (!emailCheckRegex.test(email)) {
console.log('Email is not Correct');
this.setState({
validEmail: false, disabled: true,
});
}
}
onChangePassword(password) {
this.setState({ password });
if (password.length > 5) {
//Password has to be at least 5 characters long
this.setState({
validPassword: true,
});
} else if (password.length <= 5) {
this.setState({
validPassword: false,
});
}
}
render() {
const { showpass, email, password, validEmail, validPassword, disabled } = this.state;
const opacityStyle = disabled ? 0.2 : 1;
return (
<View style={styles.inputWrapper}>
<Text style={styles.loginTextStyle}>Email</Text>
<View style={validEmail ? styles.inputCon : styles.inputConError}>
<TextInput
placeholderTextColor='#343845'
style={styles.inputField}
onChangeText={this.onChangeEmail.bind(this)}
value={email}
keyboardType='email-address'
autoCorrect={false}
// placeholder={placeholder}
underlineColorAndroid="transparent"
/>
</View>
</View>
<View style={styles.inputWrapper}>
<Text style={styles.loginTextStyle}>Password</Text>
<View style={validPassword ? styles.inputCon : styles.inputConError}>
<TextInput
placeholderTextColor='#343845'
style={styles.inputField}
onChangeText={this.onChangePassword.bind(this)}
value={password}
keyboardType='email-address'
autoCorrect={false}
// placeholder={placeholder}
secureTextEntry={showpass}
underlineColorAndroid="transparent"
returnKeyType='done'
/>
</View>
<TouchableOpacity
style={[{ opacity: opacityStyle }, styles.btnStyle]}
disabled={disabled}
>
<Text style={styles.loginStyle}>Login</Text>
</TouchableOpacity>
解决方案
disabled={this.state.email === '' || this.state.password === ''}
你是这个意思吗?
推荐阅读
- django - Django REST Serializer 使用错误的模型进行序列化
- python - python文件写入程序运行时如何更新桌面上的文件大小
- javascript - 使用 d3.js 更新表数据
- c# - C#捕获从不在进程中的函数返回的异常?
- r - 如何设置仅在输入 3 时才显示集合向量的函数?
- javascript - 如果 URI 没有改变,例如在单页应用程序上,如何检测用户是否在新页面上?
- angular - Angular Kendo UI 全局访问
- php - 内连接循环通过
- git - 如何 git rebase 从另一个分支直接到 master 分支?
- javascript - 受控数字比例映射