Click here to Skip to main content
11,926,933 members (51,364 online)
Click here to Skip to main content
Add your own
alternative version


42 bookmarked

TextBox which accepts only numbers

, 15 Jul 2005
Rate this:
Please Sign up or sign in to vote.
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

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
ahmed ahmed hassan4-Apr-12 13:30
memberahmed ahmed hassan4-Apr-12 13:30 
QuestionFilterNumeric() function source code Pin
cito2k521-Sep-11 6:37
membercito2k521-Sep-11 6:37 
Generallimiting a multiline textbox in Pin
zohra_jalal25-Jan-10 1:25
memberzohra_jalal25-Jan-10 1:25 
Questionwhat if user put text by copy and paste? Pin
Stefano Manni28-Sep-08 22:31
memberStefano Manni28-Sep-08 22:31 
Questionwhat About masking Pin
Umer Khan17-Jul-07 22:49
memberUmer Khan17-Jul-07 22:49 
GeneralDocumentation! Pin
JLeitner3-Aug-05 15:52
memberJLeitner3-Aug-05 15:52 
GeneralFirefox Pin
St€v€n16-Jul-05 4:10
memberSt€v€n16-Jul-05 4:10 
GeneralRe: Firefox Pin
St€v€n16-Jul-05 5:44
memberSt€v€n16-Jul-05 5:44 
GeneralRe: Firefox Pin
St€v€n16-Jul-05 6:02
memberSt€v€n16-Jul-05 6:02 
GeneralRe: Firefox Pin
pekica28-Jul-05 12:29
memberpekica28-Jul-05 12:29 
GeneralRe: Firefox Pin
St€v€n28-Jul-05 13:30
memberSt€v€n28-Jul-05 13:30 
GeneralRe: Firefox Pin
pekica28-Jul-05 13:54
memberpekica28-Jul-05 13:54 
GeneralRe: Firefox Pin
DeKale28-Jul-05 22:32
memberDeKale28-Jul-05 22:32 
GeneralRe: Firefox Pin
DeKale11-Aug-05 7:59
memberDeKale11-Aug-05 7:59 
Generalanother update Pin
lei_00830-Aug-05 4:36
memberlei_00830-Aug-05 4:36 
GeneralRe: another update Pin
DeKale30-Aug-05 11:47
memberDeKale30-Aug-05 11:47 
GeneralCulture info Pin
St€v€n16-Jul-05 2:39
memberSt€v€n16-Jul-05 2:39 
GeneralRe: Culture info Pin
St€v€n28-Jul-05 13:25
memberSt€v€n28-Jul-05 13: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.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151126.1 | Last Updated 15 Jul 2005
Article Copyright 2005 by Tadas Budvytis
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid