Thursday, June 14, 2007

Check Time with a Regular Expression in javascript

copy following code to save a html file, and run it.
<html>
 <body>
  <script type="text/javascript">
  function check_time()
  {
    var elem = document.getElementById('timeStr');
    var orig_str = elem.value;
    //24 hour
    var _reg = new RegExp(/^([01][0-9]|2[0-3]):[0-5][0-9]$/);
    alert(_reg.test(orig_str));
  }
  </script>
  <input type="text" id="timeStr">
  <input type="button" value="test" onclick="check_time()" />
 </body>
</html>
 
Blogger Template Layout Design by [ METAMUSE ] : Code Name Gadget 1.1 Power By freecode-frecode.blogger.com & blogger.com Programming Blogs - BlogCatalog Blog Directory