最新回應

Loading...

2014年3月17日 星期一

lab 10 Regular expression I

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

 document.getElementById("exampleForm1").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>



判斷輸入是否為整數或小數



沒有留言:

張貼留言