Click here to Skip to main content
15,905,683 members
Please Sign up or sign in to vote.
1.31/5 (3 votes)
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 23-Mar-20 9:10am

C#
      <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" />
 
Share this answer
 
v2
Comments
santhu888 17-Oct-13 8:38am    
Thanx,it working
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;
}
 
Share this answer
 
 
Share this answer
 
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 -->
 
Share this answer
 
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 ...
//OTHER METHOD BY CLASS
I share this way of allowing the entry of only numbers through a class, to be able to add them to the inputs.
JavaScript
var keyNoNumbers = [8, 9, 16, 37, 38, 39, 40, 46]; // e.which( scape:8, tab:9, shift:16, direccional:37-40, suprimir:46, enter:13 )   
 
var keyboardRightNumbers  = [97, 98, 99, 100, 101, 102, 103, 104, 105]; // e.which(teclado numerico derecho : 96-105)

var generalKeysEnabled   = keyNoNumbers.concat(keyboardRightNumbers);

    $(".onlyNumbers").on('keydown', function(e){
        var str= String.fromCharCode(e.which);        
        if (!(generalKeysEnabled.includes(e.which))){
            if (!(/[0-9]/.test(str))) {
                e.preventDefault();
            }
        }
    });
 
Share this answer
 

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