2014年3月10日 星期一

Lab 8 Using browsers for programming II

要求:撰寫一個網頁能夠自動檢查輸入的密碼,長度必須至少六個字元,且必須至少包含一個數字以及一個非英文字母的字元。
---------------------

<html>
<head>
</head>
<body id="body">
<form action="javascript:void(0);" id="exampleForm">
<input type="password" id="examplePass" />
<input type="submit" />
</form>
</body>
<script>

document.getElementById("exampleForm").onsubmit =function() {
var passwordRegex = /^(?=.*\d)(?=.*[\W_]).{6,}$/;

if(!passwordRegex.test(document.getElementById("examplePass").value)){
console.log("Regex didn't match");
var notify = document.getElementById("notify");
if (notify === null){
notify = document.createElement("p");
notify.textContent = "長度必須至少六個字元,且必須至少包含一個數字以及一個非英文字母字元(無輸入順序)。"
notify.id = "notify";

var body = document.getElementById("body");
body.appendChild(notify);
}
}
};

</script>
</html>
-----------------
下圖為"未達要求"時示意圖


沒有留言:

張貼留言