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")
}
}
}
}
該函數檢查用戶是否使用了任何「非法字符」,並且該方法有效,但是當它不包含「非法」字符時什麼也沒有發生?否則if語句不運行,即使滿足它的要求?
只是一個提示:在JavaScript中,平等的運算符是'==='而不是'=='。 –
我可以建議一個正則表達式嗎? – Gary
'if(characters!= letters && ch == entry_length)'always' false',因爲'ch'不能等於'entry_length'內循環 – Grundy