0

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.

3
  • which browser you are using ? Commented Jan 15, 2015 at 6:29
  • would you check and add the html source code extracting it after render? Commented Jan 15, 2015 at 6:33
  • ther's something wrong in rendering part , see answer and run snippet , reply if it works for you Commented Jan 15, 2015 at 6:41

1 Answer 1

2

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 !

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.