This example are shows how to extend textbox so it's only accepts number as an entry. User can't enter in the textbox anything else except number. Well the process of doing that is quite simple and straightforward. All the job is done in jscript procedure which handles textbox OnKeyPress event.
Here is all the steps necessary to accomplish:
- I have created a simple jscript procedure "FilterNumeric()" which filters users keybord entries on OnKeyPress event. Anything else except numbers, and "-", "." are ignored.
- Procedure is registered on the page.
Page.RegisterClientScriptBlock ("FilterNumeric",GetNumberValidatorScript ());
- Added an extra attribute to txtNumber to handle event OnKeyPress( )
txtNumber.Attributes.Add ("onkeypress", "FilterNumeric()");
Also I have additionally added a RegularExpressionValidator in order to validate users entry on server side. It uses the following expression: (^[-]?[1-9]\d+$)|(^[-]?[1-9]$)|(^0$)|(^[-]?[1-9]\d+\"+_strSeperator+@"\d$)|(^[-]?[0-9]\.\d$)
Hope that will be helpful for someone.
hr hrPosted Sep 11, 2012, 2:36 PM
you can try below code: <script language = "javascript"> function KeyPress(e) { //Allow `Delete` & `BackSpace` if ([e.keyCode || e.which] == 8 || [e.keyCode || e.which] == 46) return true; if ([e.keyCode || e.which] < 48 || [e.keyCode || e.which] > 57) e.preventDefault ? e.preventDefault() : e.returnValue = false; return true; } </script> <asp:TextBox ID="txtPrice" onkeypress="KeyPress(event)" runat="server" Width="256px"></asp:TextBox>
krishna prasadPosted Jul 5, 2012, 8:30 AM
Create a Simple NumericTextBox Control in Windows Phone 7 Numeric Textbox-Custom Control Create an alpha-numeric textbox using jQuery InputScope For TextBox in Windows Phone 7 Resizable TextBox in ASP.NET using AjaxControlToolKit
krishna prasadPosted Jul 5, 2012, 8:29 AM
Nice Piece of Information...
Anil pandeyPosted May 3, 2012, 7:04 AM
Nice Piece of Information...
yasin hsfPosted Nov 23, 2010, 4:40 AM
czczxczxczxcxzcz
D NPosted Dec 5, 2008, 5:36 PM
Concise step by step instructions with good supporting test project