gpt4 book ai didi

javascript - 即使满足了要求, else if 语句也不会运行?

转载 作者:行者123 更新时间:2023-11-28 20:00:09 25 4
gpt4 key购买 nike

function illegal_chr(input) {
//checks 'entry_box' for illegal characters
document.getElementById("binary_p").innerHTML = "Convert binary to text or text to binary."
var entry_value = document.getElementById("entry_box").value;
var entry_length = entry_value.length
var CHARACTERS_ARRAY = []
var illegal_message_bin = "Please ensure you have only used binary."
var ILLEGAL_BIN = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "[", "]", "#", "'", ";", ",", ".", "`", "¬", "?", ">", "<", "~", "@", ":", "}", "{", "|", "/", "!", "£", "$", "%", "^", "&", "*", "(", ")", "_", "-", "+", "=", "2", "3", "4", "5", "6", "7", "8", "9"]

//adds each character to 'CHARACTERS_ARRAY'
for (var c = 0; c < entry_length; c += 1) {
character = entry_value[c];

CHARACTERS_ARRAY.push(character)
}

//loops through 'CHARACTERS_ARRAY' checking for any illegal characters
for (var ch = 0; ch < CHARACTERS_ARRAY.length; ch += 1) {
characters = CHARACTERS_ARRAY[ch];

for (var ill = 0; ill < ILLEGAL_BIN.length; ill += 1) {
letters = ILLEGAL_BIN[ill]

if (characters == letters) {
document.getElementById("binary_p").innerHTML = illegal_message_bin;
}

else if (characters != letters && ch == entry_length) {
alert("test")
}
}
}
}

该函数检查用户是否使用了任何“非法字符”,这可以工作,但是当它不包含“非法”字符时,什么也不会发生?

最佳答案

请注意,您可以使用正则表达式,如 @duffymo 所说:

function isHex(str) {
return str.match(/^[a-f0-9]{6}$/i) !== null;
}

这会检查二进制数(至少一个 0 或 1):

function isBin(str) {
return str.match(/^[0,1].*$/) !== null;
}

关于javascript - 即使满足了要求, else if 语句也不会运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21797511/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com