[英]Required any one field validation in react js
我想要 email id 或电话号码所需的任何一个文本字段。 如果该字段应该在网格项中,我应该编写什么代码。
如果电话无效,您可以使用条件渲染到 email 并在 email 无效时渲染电话文本字段。
const App = () => {
const [isEmailValid, setIsEmailValid] = useState(false);
const [isPhoneValid, setIsPhoneValid] = useState(false);
const [email, setEmail] = useState("");
const [phone, setPhone] = useState("");
return (
<div>
{ isPhoneValid ? null : (
<input
type="email"
placeholder="Enter Email"
onChange={event => {
setIsEmailValid(true)
setEmail(event.target.value)
}
}
) }
{ isEmailValid ? null : (
<input
type="number"
placeholder="Enter number"
onChange={event => {
setIsPhoneValid(true)
setPhone(event.target.value)
}
}
) }
</div>
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.