Wrox Programmer Forums Decimal check
 | Search | Today's Posts | Mark Forums Read
 Javascript General Javascript discussions.
 Welcome to the p2p.wrox.com Forums. You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
November 25th, 2004, 11:12 AM
 Authorized User Join Date: Sep 2004 Location: West Midlands, , United Kingdom. Posts: 27 Thanks: 0 Thanked 0 Times in 0 Posts
Decimal check

Hi Guys,
Can anyone pls tell me how can I check if the number input by user is decimal if it is then it should only be upto 2 decimal places.

I can check if it is decimal by isdecimal() but how can I check the number of digits from decimal point.

Any help would be very much appreciate.
cheers

November 25th, 2004, 11:30 AM
 Friend of Wrox Join Date: Jul 2003 Location: , , United Kingdom. Posts: 683 Thanks: 0 Thanked 1 Time in 1 Post

Hi,

One way is to use a regular expression - just pass the number & required decimal places to this method...
Code:
```function CheckDecimal(pNum, pDp){
return new RegExp("^\\d*\\.\\d{0," + pDp + "}\$").test(pNum);
}```
HTH,

Chris

November 25th, 2004, 11:48 AM
Wrox Author
 Points: 9,763, Level: 42
 Activity: 0%

Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts

Good idea, perhaps with a minus?
Code:
`"^-?\\d*\\.\\d{0," + pDp + "}\$"`

--

Joe (Microsoft MVP - XML)
November 25th, 2004, 11:51 AM
 Authorized User Join Date: Sep 2004 Location: West Midlands, , United Kingdom. Posts: 27 Thanks: 0 Thanked 0 Times in 0 Posts

Hi Chris,
The function which you mentioned:
function CheckDecimal(pNum, pDp){
return new RegExp("^\\d*\\.\\d{0," + pDp + "}\$").test(pNum);

Since I need two decimal places does that infer that pdp = 2
and what does "d" stand in RegExp .
Is test and RegExp inbuilt functions in JavaScript.

November 25th, 2004, 12:03 PM
 Friend of Wrox Join Date: Jul 2003 Location: , , United Kingdom. Posts: 683 Thanks: 0 Thanked 1 Time in 1 Post

Hi,

Yep, make pDp = 2 for 2 decimal places.

\d means a decimal character in regular expression syntax, but as it's being passed to the expressions constructor as a string, it needs to be escaped and hence becomes \\d.

new RegExp creates a regular expression object and test is one of it's methods/functions - well worth checking out if you're going to do much in the way of string manipulation or validation.

Cheers,

Chris

 Similar Threads Thread Thread Starter Forum Replies Last Post How to check programmatically check the password? thomaskelly ASP.NET 1.0 and 1.1 Basics 1 May 16th, 2008 08:49 PM Connect to VSS check-in Check-out Programatically rhd110 General .NET 6 August 12th, 2007 07:46 AM decimal data type all 0's after decimal gobotsoup SQL Server ASP 1 February 28th, 2007 02:32 PM Help 'Check ListView' vs 'Check ListBox' MikeY C# 1 February 24th, 2005 02:20 PM How to check if a number has a decimal snowy0 VB How-To 5 August 5th, 2004 02:38 PM