Click here to Skip to main content
6,629,377 members and growing! (21,283 online)
Email Password   helpLost your password?
Web Development » Client side scripting » General     Intermediate License: The Code Project Open License (CPOL)

Number Validation and Formatting using Javascript

By Shoki

Numberic value validation and formatting using Javascript
Javascript, Dev
Posted:8 Apr 2008
Views:16,479
Bookmarked:6 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
4 votes for this article.
Popularity: 1.67 Rating: 2.78 out of 5
1 vote, 33.3%
1

2

3
1 vote, 33.3%
4
1 vote, 33.3%
5

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


Member

Occupation: Team Leader
Location: India India

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
GeneralGreat work Pinmemberismail Hingoro0:09 8 Sep '09  
GeneralGreat script PinmemberMember 46162474:43 23 Jan '09  
GeneralThank you PinmemberMember 306128619:32 12 Jun '08  
Generaland one wish.. Pinmemberberkcan17:48 7 May '08  
GeneralRe: and one wish.. PinmemberShoki8:58 12 Jun '08  
GeneralHey thanks ! Pinmemberberkcan17:22 7 May '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 8 Apr 2008
Editor:
Copyright 2008 by Shoki
Everything else Copyright © CodeProject, 1999-2009
Web11 | Advertise on the Code Project