免費開始練習
地特四等申論題 111年 [資訊處理] 程式設計概要

第 二 題

以下 JavaScript 程式,其功能為驗證輸入密碼是否合法,其合法規則為:
⑴密碼不為空;
⑵密碼長度必須是 6 或 7 或 8;
⑶密碼包含至少一個大寫與一個小寫英文字母;
若違反規則⑴,跳出視窗顯示"Empty",若違反規則⑵,跳出視窗顯示"6~8 char",若違反規則⑶,跳出視窗顯示"Uppercase and lowercase",若無違反規則,則在頁面密碼後面顯示「ok」。
完成程式碼空格(I)、(II)、(III)、(IV)、(V),使執行正確。(25 分)
<html> <head> <title> Verification</title> </head>
    <script>
    function verifyPass() {
        var password = document.getElementById("password"). (I) ;
        if(password == (II) ) {
            alert("Empty");
            return false;
        }
        else if ((password.length < (III) ) || (password.length > (IV) )) {
            alert("6~8 char");
            return false;
        }
        else if (!(password.match(/^(?=.*\​d)(?=.*[a-z])(?=.*[A-Z])/))) {
            alert("Uppercase and lowercase");
            return false;
        }
        else {
            document.getElementById("M"). (V) = "ok";
            return false;
        }
    }
    </script>
    <body>
        <h1 style="color:blue">Password</h1>
        <h3>Password Verification </h3>
        <form onsubmit ="return verifyPass()">
            <td> Enter Password : </td>
            <input type = "password" id = "password" value = "">
            <span id = "M" style="color:red"> </span>
            <br><br>
            <input type = "submit" value = "Submit">
        </form>
    </body>
</html>
📝 此題為申論題

思路引導 VIP

看到此題,應先辨識出這是一題 JavaScript DOM 操作與表單驗證題。解題關鍵在於熟悉如何取得輸入框的值(value)、字串長度屬性(length)的邊界條件判斷,以及如何動態修改特定 HTML 元素內的顯示文字(innerHTML)。

🤖
AI 詳解 AI 專屬家教

(I)value 說明:透過 .value 屬性取得 HTML <input id="password"> 元素中使用者輸入的文字資料。 (II)"" (或 '')

▼ 還有更多解析內容