Click here to Skip to main content
14,883,976 members
Articles / Web Development / ASP.NET
Posted 15 Jul 2005


42 bookmarked

TextBox which accepts only numbers

Rate me:
Please Sign up or sign in to vote.
3.06/5 (12 votes)
15 Jul 2005
An article on how to allow users to enter only numbers in a TextBox.


In this example, I'll show you how to extend ASP.NET textbox so that it accepts only numbers as input. Users will not be allowed to enter anything other than numbers in the textbox.

Using the code

Well, the process of doing this is quite simple and straightforward. The entire job is done in the Jscript procedure which handles the textbox's OnKeyPress event. Here are the steps involved to accomplish this:

  1. I have created is a simple Jscript procedure 'FilterNumeric()' which filters users keyboard entries on OnKeyPress event. Anything other than numbers, and '-', '.' are ignored.
  2. The procedure is registered on the page.
    Page.RegisterClientScriptBlock ("FilterNumeric",
  3. I have added an extra attribute to txtNumber control to handle the event OnKeyPress().
    txtNumber.Attributes.Add ("onkeypress", "FilterNumeric()");

I have added a RegularExpressionValidator to validate user entries on the server side. It uses the following expression:


and it allows: {123134456;4341643.2}.

Hope it helps someone!


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Tadas Budvytis
Team Leader
Canada Canada
No Biography provided

Comments and Discussions

GeneralMy vote of 5 Pin
ahmed ahmed hassan 4-Apr-12 12:30
Memberahmed ahmed hassan 4-Apr-12 12:30 
QuestionFilterNumeric() function source code Pin
NetCito21-Sep-11 5:37
MemberNetCito21-Sep-11 5:37 
Generallimiting a multiline textbox in Pin
zohra_jalal25-Jan-10 0:25
Memberzohra_jalal25-Jan-10 0:25 
Questionwhat if user put text by copy and paste? Pin
Stefano Manni28-Sep-08 21:31
MemberStefano Manni28-Sep-08 21:31 
Questionwhat About masking Pin
Umer Khan17-Jul-07 21:49
MemberUmer Khan17-Jul-07 21:49 
GeneralDocumentation! Pin
JLeitner3-Aug-05 14:52
MemberJLeitner3-Aug-05 14:52 
GeneralFirefox Pin
DeKale16-Jul-05 3:10
MemberDeKale16-Jul-05 3:10 
GeneralRe: Firefox Pin
16-Jul-05 4:44
suss16-Jul-05 4:44 
GeneralRe: Firefox Pin
16-Jul-05 5:02
suss16-Jul-05 5:02 
GeneralRe: Firefox Pin
mikker_12328-Jul-05 11:29
Membermikker_12328-Jul-05 11:29 
GeneralRe: Firefox Pin
28-Jul-05 12:30
suss28-Jul-05 12:30 
GeneralRe: Firefox Pin
mikker_12328-Jul-05 12:54
Membermikker_12328-Jul-05 12:54 
GeneralRe: Firefox Pin
DeKale28-Jul-05 21:32
MemberDeKale28-Jul-05 21:32 
GeneralRe: Firefox Pin
DeKale11-Aug-05 6:59
MemberDeKale11-Aug-05 6:59 
Generalanother update Pin
lei_00830-Aug-05 3:36
Memberlei_00830-Aug-05 3:36 
GeneralRe: another update Pin
DeKale30-Aug-05 10:47
MemberDeKale30-Aug-05 10:47 
GeneralCulture info Pin
16-Jul-05 1:39
suss16-Jul-05 1:39 
GeneralRe: Culture info Pin
28-Jul-05 12:25
suss28-Jul-05 12:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.