Restricting Key Stokes in text box
Hello
I have an web application (asp.net + C#)
I have an textbox that is always filled with numeric value.
i want to restrict other all the key press except the numeric key & - .
as for the phone the hyphen is required.
How to implement it?
I tried but no success.
Any Idea?
Re: Restricting Key Stokes in text box
As pressing a key in a web-browser is a client side event, so you will have to use Client Side Javascript to do that.
Here is a script that will allow only numbers in a textbox
Code:
<SCRIPT language=javascript>
function isNumberKey(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 45 && (charCode < 48 || charCode > 57))
return false;
return true;
}
</SCRIPT>
Remember in your HTML part of the TextBox you will have to write this onkeypress="return isNumberKey(event)"