Click here to Skip to main content
15,867,704 members
Articles / Web Development / ASP.NET
Article

TextBox which accepts only numbers

Rate me:
Please Sign up or sign in to vote.
3.06/5 (12 votes)
15 Jul 2005 176.2K   3.2K   42   18
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.
    JavaScript
    Page.RegisterClientScriptBlock ("FilterNumeric",
                                     GetNumberValidatorScript());
  3. I have added an extra attribute to txtNumber control to handle the event OnKeyPress().
    JavaScript
    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


Written By
Team Leader
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
ahmed ahmed hassan 4-Apr-12 12:30
ahmed ahmed hassan 4-Apr-12 12:30 
QuestionFilterNumeric() function source code Pin
NetCito21-Sep-11 5:37
NetCito21-Sep-11 5:37 
Generallimiting a multiline textbox in c#.net Pin
zohra_jalal25-Jan-10 0:25
zohra_jalal25-Jan-10 0:25 
Questionwhat if user put text by copy and paste? Pin
Stefano Manni28-Sep-08 21:31
Stefano Manni28-Sep-08 21:31 
Questionwhat About masking Pin
Umer Khan17-Jul-07 21:49
Umer Khan17-Jul-07 21:49 
GeneralDocumentation! Pin
JLeitner3-Aug-05 14:52
JLeitner3-Aug-05 14:52 
GeneralFirefox Pin
DeKale16-Jul-05 3:10
DeKale16-Jul-05 3:10 
GeneralRe: Firefox Pin
16-Jul-05 4:44
suss16-Jul-05 4:44 
GeneralRe: Firefox Pin
16-Jul-05 5:02
suss16-Jul-05 5:02 
GeneralRe: Firefox Pin
mikker_12328-Jul-05 11:29
mikker_12328-Jul-05 11:29 
GeneralRe: Firefox Pin
28-Jul-05 12:30
suss28-Jul-05 12:30 
GeneralRe: Firefox Pin
mikker_12328-Jul-05 12:54
mikker_12328-Jul-05 12:54 
GeneralRe: Firefox Pin
DeKale28-Jul-05 21:32
DeKale28-Jul-05 21:32 
GeneralRe: Firefox Pin
DeKale11-Aug-05 6:59
DeKale11-Aug-05 6:59 
Generalanother update Pin
lei_00830-Aug-05 3:36
lei_00830-Aug-05 3:36 
GeneralRe: another update Pin
DeKale30-Aug-05 10:47
DeKale30-Aug-05 10:47 
GeneralCulture info Pin
16-Jul-05 1:39
suss16-Jul-05 1:39 
GeneralRe: Culture info Pin
28-Jul-05 12:25
suss28-Jul-05 12:25 
I'm sorry about that. The code in the previous post is actually wrong. What it should have been was this:

<code>protected CompareValidator BuildNumberValidator(string iIDArg,string strErrArg)
{
	CompareValidator cv = new CompareValidator();
	cv.ControlToValidate = iIDArg;
	cv.Operator = ValidationCompareOperator.DataTypeCheck;
	cv.Type = ValidationDataType.Double;
	return cv;
} </code>


In other words, the ValidationCompareOperator should have been DataTypeCheck and not Equal.

Note that the Validators in ASP.NET work well with the users culture, so some difficult RegExp are not necessarily.

Some other improvements could be made to the control.
1. Try using a StringBuilder (or even better JavaScriptBuilder) for the GetNumberValidatorScript() function. += operations are quite expensive in C#.
2. Create a NumericTextBox control that derives from TextBox, implements the GetNumberValidatorScript() function and calls the Page.RegisterClientScriptBlock() and this.Attributes.Add() in the OnInit() event. This way you have a more reusable solution.

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.