Click here to Skip to main content
Click here to Skip to main content
Alternative Tip/Trick

Text box to accept only number

, 16 Jan 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
This "Decimal TextBox" is an extension to the original solution, which allows entering the negative sign (chr 45) and decimal point (chr 46) in addition to the digits 0...9 and backspace (chr 8) using just client scripting to perform the client-side validation: <script...
This "Decimal TextBox" is an extension to the original solution, which allows entering the negative sign (chr 45) and decimal point (chr 46) in addition to the digits 0...9 and backspace (chr 8) using just client scripting to perform the client-side validation:
<html>
  <head>
    <script type="text/javascript">
        function IsDecimal(e)
        {
          var chrCode = (e.which) ? e.which : event.keyCode
          return (chrCode > 47 && chrCode < 58) || 
	         chrCode == 8  ||
	         chrCode == 45 ||
	         chrCode == 46 ? true: false;
        }
      </script>
   </head>
   <body>
      <input type="text" id="txtDecimal"  önkeypress="return IsDecimal(event);" />
   </body>
</html>
Upon necessity, other ASCII codes could be added in a pretty intuitive and straightforward way in order to allow additional functionality associated with, e.g. TAB, Enter keys, etc.
 
This simple solution provides a good deal of validation on a single-character level. Further validation could be achieved on the entire textBox content by applying, for example, isFinite() and parseFloat() Javascript functions and providing some visual response (e.g. changing the textBox background color to red) in case of !isNaN.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

DrABELL
President Infosoft International Inc
United States United States
Dr. A. Bell has 20+ years of Software and Electrical Engineering experience: Win/Web veteran published 300+ articles and authored 37 inventions, currently focused on: Windows 7/8, HTML5, CSS3, jQuery, .NET, ASP.NET, WPF, C#, SQL, 'Big Data', AI, Speech Technology and Mobile apps. He participated in App Innovation Contest (AIC 2102/2013) with several winning submissions. Sample pubs follow:
  1. Edumatter M12: School Math Calculators and Equation Solvers (contest winner)
  2. Engineering Calculator VOLTA-2013 (contest winner)
  3. HTML5 Best Practices: Table formatting via CSS3
  4. Engineering Calculator VOLTA-814D
  5. SQL generates large data sequence
  6. Aggregate Product function extends SQL
  7. Watch Your Mouse!
  8. Top-100 Digital Cameras
  9. WebTV Project: Embedded YouTube Player (Goog #1 YouTube API for ASP.NET)
Dr. Bell is personally credited for 10+ Enterprise level projects w/total code base exceeding 250k lines.
Follow on   Twitter

Comments and Discussions

 
SuggestionWhy so complicated try this code PinmemberMember 1128253915-Dec-14 3:39 
GeneralRe: This simple solution provides a good deal of validation on a... PinmemberDrABELL25-Apr-11 11:53 
GeneralRe: This simple solution provides a good deal of validation on a... PinmemberDrABELL25-Apr-11 11:45 
GeneralRe: Thanks for the reply. Fixing point 3: It does not check for ... PinmemberNaerling25-Apr-11 4:03 
GeneralRe: Hi Naerling, Thanks for your message. Please find my commen... PinmemberDrABELL25-Apr-11 3:39 
GeneralReason for my vote of 4 Good one PinmemberPrasanta_Prince1-Jun-11 8:29 
GeneralRe: Many thanks, Prasanta! Best regards, Alex. PinmemberDrABELL1-Jun-11 14:25 
GeneralI am not familiar with javascript/asp.net (whichever this is... PinmemberNaerling24-Apr-11 22:47 
GeneralRe: Hi Naerling, Thanks for your message. Please find my comment... PinmemberDrABELL25-Apr-11 3:35 

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
Web01 | 2.8.141220.1 | Last Updated 16 Jan 2012
Article Copyright 2011 by DrABELL
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid