Javascript 如何验证用户从数组中的索引输入
我在一个数组中有一组硬编码的邮政编码 如果用户输入一个zipcode,并且该zipcode与数组中的任何zipcode匹配,则表单中会出现其他输入字段。如果不匹配,他们会收到一条显示“zipcodes不匹配”的消息 如何正确验证用户输入以匹配数组中的数据?我正在使用react.js 这是我的数组Javascript 如何验证用户从数组中的索引输入,javascript,reactjs,antd,Javascript,Reactjs,Antd,我在一个数组中有一组硬编码的邮政编码 如果用户输入一个zipcode,并且该zipcode与数组中的任何zipcode匹配,则表单中会出现其他输入字段。如果不匹配,他们会收到一条显示“zipcodes不匹配”的消息 如何正确验证用户输入以匹配数组中的数据?我正在使用react.js 这是我的数组 const zipCode = [ 75013, 75002, 75252,75287,75035,75069,75070] here is my function const correctZi
const zipCode =
[ 75013, 75002, 75252,75287,75035,75069,75070]
here is my function
const correctZip = () =>{
if("" !== zipCode){
return("Sorry we currently do not service your area at this time")
}
}
这是我的输入字段
我正在使用antd进行造型
<Col span={4} justify="center" align="left">
<Form.Item label="Please enter your zip code" name="zip code">
</Input onChange={zipCode}>
</Form.Item>
</Col>
如果要将数组valeus存储为字符串,如果将它们存储为num类型,则会使用前导0删除邮政编码中的0 但是你可以通过一个简单的循环来完成你想要的。为了演示的目的,我将用户InputZip称为“UserInputZip”
const correctZip=()=>{
//循环遍历数组中的每个元素
对于(i=0;i
就收集用户输入而言,您可能希望使用state。以下是react提供的一个资源,介绍如何做到这一点
const correctZip = () =>{
// loop through each element in your array
for (i = 0; i < zipCode.length; i++) {
const currentZip = zipCode[i]
// if the user input equals one of the values in your array, return true
if(userInputedZip === currentZip {
return true;
}
}
// if none of the zips match just return false
return false;
}