2014年3月17日 星期一

Lab 10 Regular expression I

所求:
撰寫一個網頁能夠自動檢查輸入的字串是否為整數或小數(例如 12.34, -1.2, +0.02, .30)。

---------------------

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

document.getElementById("exampleForm").onsubmit =function() {
var passwordRegex = /^[+-]?\d*(\.\d*)?$/;

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>
-------------------------------------------------
下圖為輸入正確示意圖


沒有留言:

張貼留言