Click here to Skip to main content
Click here to Skip to main content
Go to top

Javascript Force Numeric Input

, 4 Oct 2008
Rate this:
Please Sign up or sign in to vote.
A JavaScript function which rejects non-numeric key strokes

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)

Share

About the Author

Gary Dryden
Software Developer (Senior)
Canada Canada
No Biography provided

Comments and Discussions

 
GeneralMy vote of 1 Pinmembermetafr19-Mar-10 0:16 
GeneralCross-browser support PinmemberSirArthur29-Jan-10 15:01 
GeneralShift+Number Key Pinmemberumay14-Apr-09 1:08 
GeneralRe: Shift+Number Key PinmemberYongseop Kim1-Mar-12 19:19 
Generalused somehow Pinmemberykorotia24-Nov-08 9:55 
GeneralFix To Include numberpad PinmemberMember 435567011-Nov-08 10:20 
GeneralRe: Fix To Include numberpad Pinmemberdr_em5-May-09 20:58 
GeneralCross-browser support Pinmemberpaul.m.wilkins5-Oct-08 13:46 

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 | Mobile
Web02 | 2.8.140926.1 | Last Updated 4 Oct 2008
Article Copyright 2008 by Gary Dryden
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid