2014年3月17日 星期一

Lab 12 Regular expression in action

題目:Write a form to send a query to Google maps. If the input is not in the format of coordinates, display an error and ask the user to retry. For example, an incorrect input may look like 12.a2,21.22

===============================
<html>
<head>
</head>
<body id="body">
<form action="https://maps.google.com/" id="exampleForm" method="get" name="q" onsubmit="return check()">
<input id="examplePass" name="q" type="text" />
<input type="submit" value="submit" />
</form>
</body>
<script>

document.getElementById("exampleForm").onsubmit = function(){
var passwordRegex =/^([+-]?)(180|(1[0-7][0-9](\.)?(\d)?(\d)?(\d)?(\d)?(\d)?(\d)?)|([0-9]?[0-9](\.)?(\d)?(\d)?(\d)?(\d)?(\d)?(\d)?)),([+-]?)(90|(([0-8]?[0-9])|([0-8]?[0-9](\.)(\d)(\d)?(\d)?(\d)?(\d)?(\d)?)))$/;

if(!passwordRegex.test(document.getElementById("examplePass").value)){
console.log("Regex didn't match");
return false;
var notify = document.getElementById("notify");
if(notify === null){
notify = document.createElement("p");
notify.textContent = "ERROR";
alert("錯誤!Incorrect format");
notify.id ="notify";
var body =document.getElementById("body");
body.appendChild(notify);
}
}
};
</script>
</html>


沒有留言:

張貼留言