5,695,118 members and growing! (11,870 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, .NET, ASP.NET, Visual Studio, VS.NET2003, Dev

Posted: 15 Jul 2005
Updated: 15 Jul 2005
Views: 64,637
Bookmarked: 29 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
11 votes for this Article.
Popularity: 3.06 Rating: 2.94 out of 5
2 votes, 18.2%
1
4 votes, 36.4%
2
0 votes, 0.0%
3
0 votes, 0.0%
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


Thanx to Andrew Neprokin for a help
to implement demo project.
Occupation: Web Developer
Location: Canada Canada

Other popular ASP.NET Controls articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 15 of 15 (Total in Forum: 15) (Refresh)FirstPrevNext
Generalwhat if user put text by copy and paste?memberStefano Manni22:31 28 Sep '08  
Questionwhat About maskingmemberUmer Khan22:49 17 Jul '07  
GeneralDocumentation!memberJLeitner15:52 3 Aug '05  
GeneralFirefoxmemberSt€v€n4:10 16 Jul '05  
GeneralRe: FirefoxmemberSt€v€n5:44 16 Jul '05  
GeneralRe: FirefoxmemberSt€v€n6:02 16 Jul '05  
GeneralRe: Firefoxmemberpekica12:29 28 Jul '05  
GeneralRe: FirefoxmemberSt€v€n13:30 28 Jul '05  
GeneralRe: Firefoxmemberpekica13:54 28 Jul '05  
GeneralRe: FirefoxmemberDeKale22:32 28 Jul '05  
GeneralRe: FirefoxmemberDeKale7:59 11 Aug '05  
Generalanother updatememberlei_0084:36 30 Aug '05  
GeneralRe: another updatememberDeKale11:47 30 Aug '05  
GeneralCulture infomemberSt€v€n2:39 16 Jul '05  
GeneralRe: Culture infomemberSt€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-2008
Web18 | Advertise on the Code Project