Coding man
Coding man

Reputation: 967

Allow only numbers in Textbox not working : ASP.NET

I need only numbers in textbox. This is the javascript code that I am using :

 function onlyNumbers(evt) {
     evt = (evt) ? evt : window.event;
     var charCode = (evt.which) ? evt.which : evt.keyCode;
     if (charCode > 31 && (charCode < 48 || charCode > 57)) {
         alert(charCode);
         return false;
     }
     return true;
 }

The ASP text box on which I am calling the above function is :

<asp:TextBox ID="txtArtCount" placeholder="Numeric" onKeypress="return onlyNumbers(event);" runat="server" Width="65px" CssClass="searchTextBoxes"></asp:TextBox>

I am using Internet Explorer8 browser. I checked in Chrome as well but it is not working.

On pressing key in textbox, alert is shown but the text gets typed in the textbox as well.

Upvotes: 0

Views: 2013

Answers (1)

Vishal Sharma
Vishal Sharma

Reputation: 2793

code works correctly , I used input type text instead asp one , check in view source if something renders incorrectly or not

function onlyNumbers(evt) {
     evt = (evt) ? evt : window.event;
     var charCode = (evt.which) ? evt.which : evt.keyCode;
     if (charCode > 31 && (charCode < 48 || charCode > 57)) {
         alert(charCode);
         return false;
     }
     return true;
 }
<input type="text" ID="txtArtCount" placeholder="Numeric" onKeypress="return onlyNumbers(event);" />

hope that helps !

Upvotes: 2

Related Questions