tags:

views:

149

answers:

2

work on C# asp.net C#.I am having textbox to enter only numbers not letters.

If someone try to enter letters then he fail to enter letter.

Length of entering a number is finite like : he can enter 100 to 200. not more than.

<asp:TextBox ID="txtLength" runat="server" onkeydown="return javascript:DigitOnly(this)" >0</asp:TextBox>

want numedic text box? where user can only input number like : int ,double not any letter.

+4  A: 

Like this:

onkeydown="return DigitOnly(event, this);"

function DigitOnly(event, elm)
{
    var key = String.fromCharCode(event.keyCode ? event.keyCode : event.which);
    return key.match(/\d/) && elm.value.length < 200;
}

You could make it better by allowing arrow keys, copy & paste, etc.

Greg
certainly it must accept arrow keys, home, end for navigation, and delete, backspace for editing. Also Greg, how about passing the value of maximum number of character user can enter instead of hardcoding 200. +1 for the nice lil method :)
Rakesh Juyal
+1  A: 

You can try javascript input masking e.g.: Masked Input Plugin by digitalBush.

o.k.w