2014年3月17日 星期一

Lab 13 Regular expression in action II

題目:

撰寫一個網頁能夠自動檢查輸入的字串是否為網址(例如
163.17.9.5
www.cycu.edu.tw
是合法網址

123.222.2
999.999.999.999
www.cycu
不是合法網址)

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

document.getElementById("exampleForm").onsubmit = function(){
var passwordRegex =/^([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])(\.)([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])(\.)([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])(\.)([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])$/;

var passwordRegex1 =/^www(\.)(.+)(\.)(.+)$/;

if(!passwordRegex.test(document.getElementById("examplePass").value)&&!passwordRegex1.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 ="Invalid"
notify.id="notify";
var body = document.getElementById("body");
body.appendChild(notify);
}
}
};

</script>
</html>

沒有留言:

張貼留言