Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my code is below

XML
<script type="text/javascript">
//        function pageLoad() {
//            alert("deltiming.innerHTML");
//        }
        function numonly(evt) {
//            debugger;
            var nbr;
            var nbr = (window.event) ? event.keyCode : evt.which;
            if (nbr >=01 && nbr <=23 ) {
                return true;
            }
            else {
                alert("Mention the time correctly");
                return false;
            }
        }
        function nummin(evt) {
            var min;
            var min = (window.event) ? event.keyCode : evt.which;
            if (min >=00 && min <= 59) {
                return true;
            }
            else {
                alert("Mention the time correctly");
                return false;
            }
        }
        function timing() {
            var deliverytime = document.getElementById("deltiming");
            var html = "<table class=\"tbl1\" align=\"center\"><tr><td>HH</td><td>MM</td></tr><tr><td><input type='text' size='1' maxlength='2' onkeypress=\"javascript:return numonly(event);\" /></td><td><input type='text' size='1' maxlength='2' onkeypress=\"javascript:return nummin(event);\" /></td></tr></table>";
            deliverytime.innerHTML = html;


        }
    </script>


when i am run this code it shows error mention the time correctly when i enter 01 in HH textbox.
Posted

Use below script for your requirement

XML
<html>>
   <HEAD>
   <SCRIPT language="Javascript">
      <!--
      function isNumberKey(evt,val)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57))
            {
  alert("Mention the time correctly");

      return false;
       }
      else
      {
         if(val>23)
      {
        return false;
          alert("Mention the time correctly");

        }
  else
      {
         return true;
       }
   }
      }
      //-->
   </SCRIPT>
   </HEAD>
   <BODY>
      <INPUT id="txtChar" onkeypress="return isNumberKey(event,this.value)" type="text" name="txtChar">
   </BODY>
</HTML>
 
Share this answer
 
Comments
vivekx2 16-Feb-12 3:51am    
thank u for ur support....But error occurs...when i am entering 33 in hour textbox it didnt return a alert message...any help
vivekx2 16-Feb-12 4:01am    
can we get this output by using comparision of textbox value instead of using ascii code
Hi, change your code with below code:
JavaScript
var min = (evt.keyCode) ? evt.keyCode : evt.which;
 
Share this answer
 
Comments
vivekx2 16-Feb-12 6:08am    
can u give me this javascript code without using ascii values and directly get value from textbox and compare it by original time....


thanks in advance
[no name] 16-Feb-12 11:54am    
you have to be check ascii value bcoz on keypress event,it is checked by ascii value only.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900