2014年3月24日 星期一

LAB13 Regular expression in action II

自動檢查輸入的字串是否為網址
第一種:1~255.0~255.0~255.1~255
第二種:www的輸入方式


<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 = /^(((([1][\d][\d])|([2][0-4][0-9])|([2][5][0-5])|([1-9][0-9])|([1-9]))\.(([1][\d][\d])|([2][0-4][0-9])|([2][5][0-5])|([1-9][0-9])|([0-9]))\.(([1][\d][\d])|([2][0-4][0-9])|([2][5][0-5])|([1-9][0-9])|([0-9]))\.(([1][\d][\d])|([2][0-4][0-9])|([2][5][0-5])|([1-9][0-9])|[1-9]))|(www\.[\w]+\.((edu)|(com)|(gov)|(org)|(idv)|(cc))(\.\w+)?))$/;



 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 = "Error. Please try a new one"
   notify.id = "notify";
 
   var body = document.getElementById("body");
   body.appendChild(notify);
  }
 }
};

 </script>
</html>

沒有留言:

張貼留言