Click here to Skip to main content
14,385,667 members
Rate this:
Please Sign up or sign in to vote.
See more:
I am taking a textbox, but this textbox accept only numbers.using java script,how can i write?please help me?
Posted
Updated 13-Sep-19 4:59am
Rate this:
Please Sign up or sign in to vote.

Solution 2

      <script type="text/javascript" language="javascript">
function validatenumerics(key) {
           //getting key code of pressed key
           var keycode = (key.which) ? key.which : key.keyCode;
           //comparing pressed keycodes

           if (keycode > 31 && (keycode < 48 || keycode > 57)) {
               alert(" You can enter only characters 0 to 9 ");
               return false;
           }
           else return true;


       }



       <asp:textbox runat="server" id="txtquantity" width="90px" onkeypress="return validatenumerics(event);" xmlns:asp="#unknown" />
   
v2
Comments
santhu888 17-Oct-13 8:38am
   
Thanx,it working
Rate this:
Please Sign up or sign in to vote.

Solution 4

try this....
function isNumberKey(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode;
if (charCode != 46 && charCode > 31
&& (charCode < 48 || charCode > 57))
return false;

return true;
}
   
Rate this:
Please Sign up or sign in to vote.

Solution 1

   
Rate this:
Please Sign up or sign in to vote.

Solution 7

Javascript For only numeric value in textbox ::

<input type="text" id="textBox" runat="server" class="form-control"                                                                              onkeydown="return onlyNos(event)" tabindex="0" />

<!--Only Numeric value in Textbox Script -->
    <script type="text/javascript">
        function onlyNos(e, t) {
            try {
                if (window.event) {
                    var charCode = window.event.keyCode;
                }
                else if (e) {
                    var charCode = e.which;
                }
                else { return true; }
                if (charCode > 31 && (charCode < 48 || charCode > 57)) {
                    return false;
                }
                return true;
            }
            catch (err) {
                alert(err.Description);
            }
        }
    </script>
    <!--Only Numeric value in Textbox Script -->
   
Comments
Richard Deeming 20-Feb-19 9:47am
   
An unexplained code-dump isn't really a solution. How about explaining what your code provides that the existing (and accepted) solutions don't?
OriginalGriff 2-Sep-19 10:55am
   
"rep points" I suspect ...

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100