Click here to Skip to main content
Licence CPOL
First Posted 8 Apr 2008
Views 41,047
Downloads 431
Bookmarked 8 times

Number Validation and Formatting using Javascript

By | 8 Apr 2008 | Article
Numberic value validation and formatting using Javascript

Introduction

The control uses javascript to handle number formatting as you keep typing and does the validation as well. For one of our requirement, we needed a javascript based control that did formatting as the users kept typing. Google search only yielded script that did formatting on focus change events only. So I went about creating a new control myself.

Background

The idea for this control came from one of our client requirement that needed a small javascript to have a textbox that will take big numbers. The numbers can have unlimited decimal places. Also the script will do 3 things at the same time.
1- It should only allow numbers (negative as well) and "." To be entered.
2- It should format the number as 1,234,567,789.123456789. It should execute such that the the commas are put while typing.
3- It should also only allow numbers that are as big as double can handle.

Using the code

Using the control is very easy and just needs adding the javascript code from the source page.

On the textbox on which the formatting needs to be applied, use the following code

onkeypress="return ValidateNumberKeyPress(this, event);" onkeyup="ValidateNumberKeyUp(this);"
onblur="ValidateAndFormatNumber(this)"
				

Points of Interest

Script is preety easy to read and understand. Some references that were used during the course of the script creation

License

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

About the Author

Shoki

Team Leader

India India

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
GeneralGreat work Pinmemberismail Hingoro23:09 7 Sep '09  
GeneralGreat script PinmemberMember 46162473:43 23 Jan '09  
GeneralThank you PinmemberMember 306128618:32 12 Jun '08  
Great script - made my life much easier
Generaland one wish.. Pinmemberberkcan16:48 7 May '08  
GeneralRe: and one wish.. PinmemberShoki7:58 12 Jun '08  
GeneralHey thanks ! Pinmemberberkcan16:22 7 May '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.120529.1 | Last Updated 8 Apr 2008
Article Copyright 2008 by Shoki
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid