Click here to Skip to main content
14,692,581 members
Articles » Web Development » ASP.NET Controls » General
Article
Posted 15 Jul 2005

Stats

170.2K views
3.2K downloads
42 bookmarked

TextBox which accepts only numbers

Rate me:
Please Sign up or sign in to vote.
3.06/5 (12 votes)
15 Jul 2005
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 Pin
ahmed ahmed hassan 4-Apr-12 13:30
Memberahmed ahmed hassan 4-Apr-12 13:30 
QuestionFilterNumeric() function source code Pin
NetCito21-Sep-11 6:37
MemberNetCito21-Sep-11 6:37 
Generallimiting a multiline textbox in c#.net 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
DeKale16-Jul-05 4:10
MemberDeKale16-Jul-05 4:10 
GeneralRe: Firefox Pin
16-Jul-05 5:44
suss16-Jul-05 5:44 
GeneralRe: Firefox Pin
16-Jul-05 6:02
suss16-Jul-05 6:02 
GeneralRe: Firefox Pin
mikker_12328-Jul-05 12:29
Membermikker_12328-Jul-05 12:29 
GeneralRe: Firefox Pin
28-Jul-05 13:30
suss28-Jul-05 13:30 
GeneralRe: Firefox Pin
mikker_12328-Jul-05 13:54
Membermikker_12328-Jul-05 13:54 
GeneralRe: Firefox Pin
DeKale28-Jul-05 22:32
MemberDeKale28-Jul-05 22:32 
Oeps.. I wasn't paying attention.Sleepy | :zzz: Actualy i was also thinking of posting an article based on the code on this article with my changes.

Let's look who's first Wink | ;-)
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
16-Jul-05 2:39
suss16-Jul-05 2:39 
GeneralRe: Culture info Pin
28-Jul-05 13:25
suss28-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.