Click here to Skip to main content
Licence CPOL
First Posted 4 Oct 2008
Views 21,187
Downloads 221
Bookmarked 12 times

Javascript Force Numeric Input

By Gary Dryden | 4 Oct 2008
A JavaScript function which rejects non-numeric key strokes
1 vote, 14.3%
1
1 vote, 14.3%
2
2 votes, 28.6%
3

4
3 votes, 42.9%
5
3.31/5 - 7 votes
μ 3.31, σa 2.83 [?]

Introduction

While writing the CodeProject article Multi-lingual Light Weight Report Writer with DrillDown, I needed to create a textbox which would only accept numbers.
I looked around the net and couldn't find what I wanted, so I wrote one.

Using the Code

Unzip the download into your project.

Include the following in the <head> section of your *.aspx file.

<script src="JavascriptForceNumericInput.js"></script>

The function signature is:

ForceNumericInput(This, AllowDot, AllowMinus)

To allow positive and negative numbers with decimal point, code your input statement as:

<input type="text" name="YourFieldName" id="YourFieldName" value="" 
	onkeydown="ForceNumericInput(this, true, true)" />

To allow positive and negative integers, code your input statement as:

<input type="text" name="YourFieldName" id="YourFieldName" 
	value="" onkeydown="ForceNumericInput(this, false, true)" />

To allow only positive numbers with decimal point, code your input statement as:

<input type="text" name="YourFieldName" id="YourFieldName" 
	value="" onkeydown="ForceNumericInput(this, true, false)" />

To allow only positive integers, code your input statement as:

<input type="text" name="YourFieldName" id="YourFieldName" 
	value="" onkeydown="ForceNumericInput(this, false, false)" />

You must include an id attribute or the code will not work. You do not need a name attribute unless this is a form variable which will be submitted.

Points of Interest

This function was written using the Internet Explorer event model as the Light Weight Report Writer targeted ASPX applications and most of those are written for the Internet Explorer browser.
If anyone converts the code to a cross browser environment, please let me know and I'll repost the source and give credit to the author.

History

  • 28th September, 2008: Initial version

License

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

About the Author

Gary Dryden

Software Developer (Senior)

Canada Canada

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 Pinmembermetafr1:16 19 Mar '10  
GeneralCross-browser support PinmemberSirArthur16:01 29 Jan '10  
GeneralShift+Number Key Pinmemberumay2:08 14 Apr '09  
Generalused somehow Pinmemberykorotia10:55 24 Nov '08  
GeneralFix To Include numberpad PinmemberMember 435567011:20 11 Nov '08  
GeneralRe: Fix To Include numberpad Pinmemberdr_em21:58 5 May '09  
GeneralCross-browser support Pinmemberpaul.m.wilkins14:46 5 Oct '08  

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.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120209.1 | Last Updated 4 Oct 2008
Article Copyright 2008 by Gary Dryden
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid