Click here to Skip to main content
6,822,123 members and growing! (17,749 online)
Email Password   helpLost your password?
Web Development » Client side scripting » Beginners     Beginner License: The Code Project Open License (CPOL)

Javascript Force Numeric Input

By Gary Dryden

A JavaScript function which rejects non-numeric key strokes
C#2.0, Windows, Dev
Posted:4 Oct 2008
Views:11,235
Bookmarked:12 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
5 votes for this article.
Popularity: 2.62 Rating: 3.75 out of 5

1
1 vote, 20.0%
2
1 vote, 20.0%
3

4
3 votes, 60.0%
5

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


Member

Occupation: Software Developer (Senior)
Location: Canada Canada

Other popular Client side scripting articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 6 of 6 (Total in Forum: 6) (Refresh)FirstPrevNext
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    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.

PermaLink | Privacy | Terms of Use
Last Updated: 4 Oct 2008
Editor: Deeksha Shenoy
Copyright 2008 by Gary Dryden
Everything else Copyright © CodeProject, 1999-2010
Web22 | Advertise on the Code Project