Click here to Skip to main content
6,596,602 members and growing! (19,423 online)
Email Password   helpLost your password?
Web Development » ASP.NET Controls » General     Intermediate

TextBox which accepts only numbers

By Tadas Budvytis

An article on how to allow users to enter only numbers in a TextBox.
C#, Javascript, Windows, .NET 1.1, ASP.NET, VS.NET2003, Dev
Posted:15 Jul 2005
Views:79,056
Bookmarked:37 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
11 votes for this article.
Popularity: 3.06 Rating: 2.94 out of 5
2 votes, 18.2%
1
4 votes, 36.4%
2

3

4
5 votes, 45.5%
5

Introduction

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",
                                     GetNumberValidatorScript());
  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:

(^[-]?[1-9]\d+$)|(^[-]?[1-9]$)|(^0$)|(^[-]?[1-9]\d+\.\d$)|(^[-]?[0-9]\.\d$)

and it allows: {123134456;4341643.2}.

Hope it helps someone!

License

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


Member

Occupation: Team Leader
Location: Canada Canada

Other popular ASP.NET Controls articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 15 of 15 (Total in Forum: 15) (Refresh)FirstPrevNext
Generalwhat if user put text by copy and paste? PinmemberStefano Manni22:31 28 Sep '08  
Questionwhat About masking PinmemberUmer Khan22:49 17 Jul '07  
GeneralDocumentation! PinmemberJLeitner15:52 3 Aug '05  
GeneralFirefox PinmemberSt€v€n4:10 16 Jul '05  
GeneralRe: Firefox PinmemberSt€v€n5:44 16 Jul '05  
GeneralRe: Firefox PinmemberSt€v€n6:02 16 Jul '05  
GeneralRe: Firefox Pinmemberpekica12:29 28 Jul '05  
GeneralRe: Firefox PinmemberSt€v€n13:30 28 Jul '05  
GeneralRe: Firefox Pinmemberpekica13:54 28 Jul '05  
GeneralRe: Firefox PinmemberDeKale22:32 28 Jul '05  
GeneralRe: Firefox PinmemberDeKale7:59 11 Aug '05  
Generalanother update Pinmemberlei_0084:36 30 Aug '05  
GeneralRe: another update PinmemberDeKale11:47 30 Aug '05  
GeneralCulture info PinmemberSt€v€n2:39 16 Jul '05  
GeneralRe: Culture info PinmemberSt€v€n13:25 28 Jul '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 15 Jul 2005
Editor: Rinish Biju
Copyright 2005 by Tadas Budvytis
Everything else Copyright © CodeProject, 1999-2009
Web12 | Advertise on the Code Project