Click to See Complete Forum and Search --> : Restricting Key Stokes in text box


vivekshah
June 22nd, 2006, 04:01 AM
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?

Shuja Ali
June 22nd, 2006, 04:16 AM
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 <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)"