Number only input

  • Thread starter Thread starter Nick S
  • Start date Start date
N

Nick S

Is there a simple way of only allowing numeric inputs into a textbox?

Thanks
 
you may use one of two ways:

1) when any character except numbers in entered give a TRUE value to
e.Handled,so the event will ignore the character,and that is by comparing the
ASCII of the character with numbers ASCII.

2)but a try catch blocks and parse the textbox text to integer in try block
,so it will go to the catch block when there is charactres other than numbers
entered, now in the catch block you should empty the text property in the
textbox, the result will be emptying the textbox when non-numbers characters
are entered.
 
You could use some Javascript like this:

<script language="Javascript">
function ValidateNumeric()
{
var keyCode = window.event.keyCode;
if (keyCode > 57 || keyCode < 48)
window.event.returnValue = false;
}
</script>

Or if you want to get fancier you can inherit and extend the TextBox control
with such functionality, as in this example:
http://SteveOrr.net/articles/InheritAndExtend.aspx
 
You can also use Regular expressions
Patrick
**Lots of choices for you buddy..:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top