Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/448.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将布尔测试HTML文本框输入与数组值进行比较_Javascript_Html_Forms_Textbox_Boolean - Fatal编程技术网

Javascript 将布尔测试HTML文本框输入与数组值进行比较

Javascript 将布尔测试HTML文本框输入与数组值进行比较,javascript,html,forms,textbox,boolean,Javascript,Html,Forms,Textbox,Boolean,我正在尝试在网页上创建一个文本框表单来测试用户提交的输入。输入将是网站用户的邮政编码。我想做一个预先确定的邮政编码数组,它将测试为真 如果它是真的(输入的邮政编码包含在预定的数组中),我想显示一位HTML,如果它测试为假,我想显示另一位 我四处搜索,查阅了一些JavaScript书籍(我刚刚开始学习),但没有找到答案;有人能帮我解决这个问题吗?谢谢 你可以试试这样的(可能有点不对劲,我会再检查一遍,然后再给你回复:): 这可能不是最好的方法,但我建议使用jQuery,它会使这个过程变得更简单。H

我正在尝试在网页上创建一个文本框表单来测试用户提交的输入。输入将是网站用户的邮政编码。我想做一个预先确定的邮政编码数组,它将测试为真

如果它是真的(输入的邮政编码包含在预定的数组中),我想显示一位HTML,如果它测试为假,我想显示另一位


我四处搜索,查阅了一些JavaScript书籍(我刚刚开始学习),但没有找到答案;有人能帮我解决这个问题吗?谢谢

你可以试试这样的(可能有点不对劲,我会再检查一遍,然后再给你回复:):

这可能不是最好的方法,但我建议使用jQuery,它会使这个过程变得更简单。

HTML: Javascript
必须切换到onkeyup,但除此之外,它工作得非常好。谢谢!
var zipArr = ['98671','97006'];//insert the zip/post codes here
var userInputEl = document.getElementById('userInput');//Get the element could use tag names or it would be better actually if you used jQuery but yeah
var elToShow = document.getElementById('elementToShowIfNotFound');
var otherElToShow = document.getElementById('idOfOtherelementToShow');
var userInput = userInputEl.value();//might be .text()
if(zipArr.indexOf(userInput) === -1){//-1 means it isn't found in the array
    zipArr.push(userInput);//pushes the new one onto the array        
    elToShow.style.display = 'block';//this changes the css display to block instead of it being hidden.    
}else{        
    otherElToShow.style.display= 'block';
}
<label id="input-label" class="invalid">
    ZIP code: <input id="zipcode" />
    <div class="valid-message">
        Valid
    </div>
    <div class="invalid-message">
        Invalid
    </div>
</label>
function isValidZip(z) {
    return ['12345','67890'].indexOf(z) != -1;
}

var label = document.getElementById('input-label');
var input = document.getElementById('zipcode');

input.onkeydown = function() {
    label.className = isValidZip(input.value) ? "valid" : "invalid";
}