Click here to Skip to main content
Licence 
First Posted 15 Jul 2005
Views 126,880
Bookmarked 42 times

TextBox which accepts only numbers

By | 15 Jul 2005 | Article
An article on how to allow users to enter only numbers in a TextBox.

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

Team Leader

Canada Canada

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmemberahmed ahmed hassan12:30 4 Apr '12  
QuestionFilterNumeric() function source code Pinmembercito2k55:37 21 Sep '11  
Generallimiting a multiline textbox in c#.net Pinmemberzohra_jalal0:25 25 Jan '10  
Questionwhat if user put text by copy and paste? PinmemberStefano Manni21:31 28 Sep '08  
Questionwhat About masking PinmemberUmer Khan21:49 17 Jul '07  
GeneralDocumentation! PinmemberJLeitner14:52 3 Aug '05  
GeneralFirefox PinmemberSt€v€n3:10 16 Jul '05  
GeneralRe: Firefox PinmemberSt€v€n4:44 16 Jul '05  
GeneralRe: Firefox PinmemberSt€v€n5:02 16 Jul '05  
GeneralRe: Firefox Pinmemberpekica11:29 28 Jul '05  
GeneralRe: Firefox PinmemberSt€v€n12:30 28 Jul '05  
GeneralRe: Firefox Pinmemberpekica12:54 28 Jul '05  
GeneralRe: Firefox PinmemberDeKale21:32 28 Jul '05  
GeneralRe: Firefox PinmemberDeKale6:59 11 Aug '05  
Generalanother update Pinmemberlei_0083:36 30 Aug '05  
GeneralRe: another update PinmemberDeKale10:47 30 Aug '05  
GeneralCulture info PinmemberSt€v€n1:39 16 Jul '05  
GeneralRe: Culture info PinmemberSt€v€n12:25 28 Jul '05  

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

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

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