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:
- I have created is a simple Jscript procedure '
FilterNumeric()' which filters users keyboard entries on OnKeyPress event. Anything other than numbers, and '-', '.' are ignored.
- The procedure is registered on the page.
Page.RegisterClientScriptBlock ("FilterNumeric",
GetNumberValidatorScript());
- 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!
| You must Sign In to use this message board. |
|
| | Msgs 1 to 15 of 15 (Total in Forum: 15) (Refresh) | FirstPrevNext |
|
 |
|
|
 |
|
|
 |
|
 |
I'm a complete ASP.NET nOOb, so while your solution looks complete and elegant, after peering into the Zip file I downloaded, I have *NO* idea where to start! ANy chance of getting a step-by-step set of instructions on how to include this in my project (and also on how to deploy it to clients)?
Thanks much!
Joel Leitner
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
 |
This code doesn't work in firefox. On Internet Explorer perfectly though. Are you posting an update for this?
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
 |
Okay, I've got it. This code should do the trick. Also solves the Globalisation error.
Could someone check this for other browsers? I've only checked it for IE en FF.
protected string GetNumberValidatorScript() { string _str = @" <script language=""javascript""><!-- function FilterNumeric { evt = (evt) ? evt : ((window.event) ? event : null); if (evt) { var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
var ch = String.fromCharCode(charCode); if (charCode < 32) { return true; } if((charCode <= 57) && (charCode >= 48)) { if (!evt.shiftKey) { return; } } if ((ch=='" + NumberFormatInfo.CurrentInfo.NegativeSign + "') || (ch=='" + NumberFormatInfo.CurrentInfo.NumberDecimalSeparator + @"')) { return; } if (window.event) evt.returnValue = false; else evt.preventDefault(); return false; } } --></script>"; }
and don't forget to set the this.txtNumber.Attributes.Add ("onkeypress", "FilterNumeric()"); to this.txtNumber.Attributes.Add ("onkeypress", "FilterNumeric(event)");
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
 |
No problem. Your example was very useful so I used your code in my project. While doing that I found some things that could be improved and simplified and those improvements should of course be brought back to the Code Project community.
Thanks for your article.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
It's not my article I'm thinking about writing my own seeing how there is no simple, free and good ASP.NET numeric control.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Oeps.. I wasn't paying attention. 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
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
 |
Me.Attributes.Add("onkeypress", "FilterNumeric(event)")
should be
Me.Attributes.Add("onkeypress", "return FilterNumeric(window.event)")
|
| Sign In·View Thread·PermaLink | 1.50/5 (2 votes) |
|
|
|
 |
|
|
 |
|
 |
I looked at your code and saw that you called the "CurrentInfo.NumberDecimalSeparator" while building the RegularExpressionValidator, but not while building the FilterNumeric function.
I haven't been testing it, but I suppose that "if ((ch=='-') ||(ch=='.'))" still causes problems in other Languages.
And I'm wondering why you're using your own validator instead of the CompareValidator? I suppose the code below should do the trick also:
protected CompareValidator BuildNumberValidator(string iIDArg,string strErrArg) { CompareValidator cv = new CompareValidator(); cv.ControlToValidate = iIDArg; cv.Operator = ValidationCompareOperator.Equal; cv.Type = ValidationDataType.Double; return cv; }
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
I'm sorry about that. The code in the previous post is actually wrong. What it should have been was this:
protected CompareValidator BuildNumberValidator(string iIDArg,string strErrArg) { CompareValidator cv = new CompareValidator(); cv.ControlToValidate = iIDArg; cv.Operator = ValidationCompareOperator.DataTypeCheck; cv.Type = ValidationDataType.Double; return cv; }
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.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
General
News
Question
Answer
Joke
Rant
Admin