Fazal
Fazal

Reputation: 21

check input of textboxes

I am working with asp.net and I'm using textboxes for user input. I want to make sure that the user enters digits only. I don't want to use validation controls. If maybe somehow I could use the ASCII value of the text entered by the user to compare whether it's an alphabet or not using C#. I would really appreciate your help.

Upvotes: 1

Views: 146

Answers (2)

Bala R
Bala R

Reputation: 108937

<input type="text" 
onkeypress="return (event.keyCode>= 48 && event.keyCode<= 57)" />

See example

or

<asp:TextBox ID="TextBox1" runat="server"
       onkeypress="return (event.keyCode>= 48 && event.keyCode<= 57)" />

include period . in the range if it's required.

Upvotes: 2

Forgotten Semicolon
Forgotten Semicolon

Reputation: 14100

Honestly, this is what the validation controls are good at, in this case the RegularExpressionValidator would be a good candidate.

Another option would be to use the MaskedEdit Ajax Extender.

Upvotes: 2

Related Questions