Click here to Skip to main content
Click here to Skip to main content

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.

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

Share

About the Author

Tadas Budvytis
Team Leader
Canada Canada
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 Pinmemberahmed ahmed hassan4-Apr-12 13:30 
QuestionFilterNumeric() function source code Pinmembercito2k521-Sep-11 6:37 
Generallimiting a multiline textbox in c#.net Pinmemberzohra_jalal25-Jan-10 1:25 
Questionwhat if user put text by copy and paste? PinmemberStefano Manni28-Sep-08 22:31 
Questionwhat About masking PinmemberUmer Khan17-Jul-07 22:49 
GeneralDocumentation! PinmemberJLeitner3-Aug-05 15:52 
GeneralFirefox PinmemberSt€v€n16-Jul-05 4:10 
GeneralRe: Firefox PinmemberSt€v€n16-Jul-05 5:44 
GeneralRe: Firefox PinmemberSt€v€n16-Jul-05 6:02 
GeneralRe: Firefox Pinmemberpekica28-Jul-05 12:29 
GeneralRe: Firefox PinmemberSt€v€n28-Jul-05 13:30 
GeneralRe: Firefox Pinmemberpekica28-Jul-05 13:54 
GeneralRe: Firefox PinmemberDeKale28-Jul-05 22:32 
GeneralRe: Firefox PinmemberDeKale11-Aug-05 7:59 
Generalanother update Pinmemberlei_00830-Aug-05 4:36 
GeneralRe: another update PinmemberDeKale30-Aug-05 11:47 
GeneralCulture info PinmemberSt€v€n16-Jul-05 2:39 
GeneralRe: Culture info PinmemberSt€v€n28-Jul-05 13:25 

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.

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