Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Leading zeros in currency check

Status
Not open for further replies.

Jarrod13

Programmer
Dec 18, 2008
12
I'm trying to modify the following regular expression so that leading zeros will fail the check.

Code:
if(Currency !~ /^((|-)[0-9]+\.[0-9][0-9])$/ && Currency !~ "^[ \t]*$")

For example,

if I input 00000500.25

This will pass the regex, I would like that to fail.

The following should pass:
500.25
0.00
1.15

The following should fail:
00000500.25
000.00
0001.15

Thanks for the help.

 
Jarrod,

In attempting to keep this simple.

My thought would be to have the check for the occurrence of a
[1-9] zero to many times followed by the occurrence of a [0-9] only once before the decimal.

I'll have to play with the regular expression syntax but I believe it should work the way you want.

My only other question is - If leading zeros need to be stripped why not use sed or awk to do a trim so the values would be valid?

 

Or...use the strtonum() awk function after the digit validation.
[3eyes]


----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
What if you do not want to edit the data? Just see if it exists incorrectly?
 
tcerv79,

If the data in question is formatted in fields you could use awk or a cut command to extract the field you wish to view.

But my guess is you are wishing to view the incorrect data based on some criteria and not merely output everything. Correct?


 
How about the following
Code:
if ( Currency !~ /^([1-9][0-9]*\.[0-9][0-9])$|^(0\.[0-9][0-9])$/ && Currency !~ "^[ \t]*$" )
 
Oops, I forgot to add back in the test for negatives in my previous post. Sorry
Here is what I really meant to propose
Code:
if ( Currency !~ /^(-?[1-9][0-9]*\.[0-9][0-9])$|^(-?0\.[0-9][0-9])$/ && Currency !~ "^[ \t]*$" )
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top