money regular expression [Solved]

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

money regular expression [Solved]

Post by yacahuma »

beware: regex newbie talking

why this does not work?

Code: Select all

function isValidMoney($amt)
{
    return ereg("^\$?\d{1,3}(,\d{3})*(\.\d{1,2})?", $amt);
}

$money = "$2,000,000.98";
if (isValidMoney($money))
  echo "IS VALID";
else
  echo "IS NOT VALID";
as far as I understand
^\$?start with or without a dolar sign
\d{1,3} after that 1,2 or 3 digits
(,\d{3})* followed by 0 or more groups of ,DDD (has to be always 3 digits)
(\.\d{1,2})? followed by a dot and 1 or 2 digits

is there something wrong in the logic or the call?
should I use ereg or preg_match for string validation?
if I should use preg_match , how will I use it in this example?


I am using The Regex Coach and the expression works.

Thank you
Last edited by yacahuma on Mon Nov 05, 2007 2:03 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Since when does money have to have the cents written out? :)
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

is not working

Post by yacahuma »

Is not working. I am getting that this is valid

Code: Select all

function isValidMoney($amt)
{
    return preg_match('/\$?\d{1,3}(,\d{3})*(\.\d{1,2})?/', $amt);
}

$money = "$2,000,0000000000.98";
if (isValidMoney($money))
  echo "IS VALID";
else
  echo "IS NOT VALID";
about the cent. Thats why i put (\.\d{1,2})? . so it 0 or 1 times. So it does not have to be there
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You lost the ^ ...
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

now I get a warning

Post by yacahuma »

like this?

Code: Select all

return preg_match('/^\$?\d{1,3}(,\d{3})*(\.\d{1,2})?/', $amt);
Now everything is valid
$money = "$2,000,000000000.98";
$money = "$2,000,000aaa000000.98";
User avatar
seppo0010
Forum Commoner
Posts: 47
Joined: Wed Oct 24, 2007 4:13 pm
Location: Buenos Aires, Argentina

Post by seppo0010 »

I believe you should also add a $ at the end
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You may want to look at what it's matching to adjust the expression.
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

It works, but why do i need the dollar thing?

Post by yacahuma »

thank you . this works

Code: Select all

return preg_match('/^\$?\d{1,3}(,\d{3})*(\.\d{1,2})?$/', $amt);
But why do I need the $?. I see many examples in the regex crash course in this forum and they dont have that.


Also another question
this should be valid
$money = "$20000000.98";

but I dont know how to modify the expression

Thanks a lot for the help
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The ending $ is needed to anchor the pattern to the end of line, otherwise the end can land anywhere.

As for making the second format match, you can create a separate pattern then combine it with the first afterward.
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

I have the 2 pattern

Post by yacahuma »

These are my 2 patterns.

Code: Select all

return preg_match('/^\$?\d{1,3}(,\d{3})*(\.\d{1,2})?$/', $amt);
    return preg_match('/^\$?\d*(\.\d{1,2})?$/', $amt);
How can I put them into just one call? Or should I just do

Code: Select all

return preg_match(1pattern) || preg_match(2 pattern) ;
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

solution

Post by yacahuma »

I think I got it

Code: Select all

return preg_match('/^\$?(\d*|\d{1,3}(,\d{3})*)(\.\d{1,2})?$/', $amt);
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You're getting close, but I think that could have odd matches.
Post Reply