Page 1 of 1

money regular expression [Solved]

Posted: Fri Nov 02, 2007 2:56 pm
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

Posted: Fri Nov 02, 2007 3:25 pm
by feyd
Since when does money have to have the cents written out? :)

is not working

Posted: Fri Nov 02, 2007 3:43 pm
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

Posted: Fri Nov 02, 2007 3:49 pm
by feyd
You lost the ^ ...

now I get a warning

Posted: Fri Nov 02, 2007 3:54 pm
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";

Posted: Fri Nov 02, 2007 3:59 pm
by seppo0010
I believe you should also add a $ at the end

Posted: Fri Nov 02, 2007 4:00 pm
by feyd
You may want to look at what it's matching to adjust the expression.

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

Posted: Fri Nov 02, 2007 4:17 pm
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

Posted: Fri Nov 02, 2007 4:21 pm
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.

I have the 2 pattern

Posted: Mon Nov 05, 2007 1:34 pm
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) ;

solution

Posted: Mon Nov 05, 2007 2:02 pm
by yacahuma
I think I got it

Code: Select all

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

Posted: Mon Nov 05, 2007 3:27 pm
by feyd
You're getting close, but I think that could have odd matches.