By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,198 Members | 1,727 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,198 IT Pros & Developers. It's quick & easy.

Function to check money value

P: n/a
Hi,

I am looking for a good function to check if a entered value is
a money value - meaning it's a decimal with no more then 2 digits after the point.

Thanks,

Stefan
Jul 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
I noticed that Message-ID:
<e2**************************@posting.google.com > from Stefan Richter
contained the following:

I am looking for a good function to check if a entered value is
a money value - meaning it's a decimal with no more then 2 digits after the point.


Check user notes.
http://php.net/manual/en/function.eregi.php

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #2

P: n/a
On 14 Feb 2005 05:13:03 -0800, Do****@gmx.de (Stefan Richter) wrote:
I am looking for a good function to check if a entered value is
a money value - meaning it's a decimal with no more then 2 digits after the point.


Something along the lines of:

preg_match('/^\d+(?:\.\d\d?)$/', $money_value)

Do you want to allow it to be preceded by a currency symbol? How about commas
for thousands separators, or allowing mainland-European-style separators
(points for thousand separators, comma for decimal e.g. 1.000,99)?

The above would be OK if all you want is "digits followed optionally by [dot
plus one or two digits]".

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #3

P: n/a
I assume you are talking about this function:

function check_price($price)
{
$data=split('[.]',$price);

if ( count($data) != 2 )
return "false";

if ( ctype_digit($data[0]) && ctype_digit($data[1]) && $data[0][0]
!= '0')
return "true";
else
return "false";
}

However, there are two problems:
1. I can't find this ctype_digit function in my php reference, so I
assume it doesn't exist.

2. How can I switch from german to english number format???

I thought number_format($money,2,".",",") would do it,
however, it seems that this chops of existing values after the comma /
dot.

???

Thanks,

Stefan
Jul 17 '05 #4

P: n/a
Hi, I want to go create a table with a textfield where the user can
enter an amount for a certain dynamically created article coming from
the database.

Later, I want to calculate the whole price and so on.
It works nicely to create my multi dimensional array,
however, it does not work to save the values coming from the form to
calculate the price.
What's wrong???

<form name="shop" method="post" action="index.php">
$articleTable = array();

while ($row = mysql_fetch_array($result)) {
$articleNr= $row["articleNr"];
$name= $row["name"];
$picName= $row["picName"];
$price = $row["price"];
$amount="";
$articleRow = array(
"articleNr" => $articleNr,
"name" => $name,
"picName" => $bildName,
"price " => $price ,
"amount" => $amount
);

array_push($articleTable , $articleRow );

}

foreach($articleTable as $article) {
?>
<input type="text" name="article[amount]" size="2" maxlength="2"
value="<? $article[amount] ?>"></td>
}

<?
}
?>
</form>
Jul 17 '05 #5

P: n/a
Thanks for your reply Andy.
-------

Well, first of all it needs to be able to be saved my mysql.
Can mysql understand thousands seperators?
If not, I would need a function to take out these seperators.

About . vs ,:

Well, this function is for Germany,
so I would need it the other way round,
but the best was if it would except both of course.
And no, I don't need a currency symbol, as I am always using euros so
I am not saving that in the database.
Thanks,

Stefan
Jul 17 '05 #6

P: n/a
I noticed that Message-ID:
<e2**************************@posting.google.com > from Stefan Richter
contained the following:
Later, I want to calculate the whole price and so on.
It works nicely to create my multi dimensional array,
however, it does not work to save the values coming from the form to
calculate the price.
What's wrong???


There is no code there which does that. And if you already have an
array called $row why change it to an array called $articleRow It seems
identical apart from the variable $bildName which appears from nowhere.

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #7

P: n/a
Geoff Berrow <bl******@ckdog.co.uk> wrote in message news:<cb********************************@4ax.com>. ..
I noticed that Message-ID:
<e2**************************@posting.google.com > from Stefan Richter
contained the following:
Later, I want to calculate the whole price and so on.
It works nicely to create my multi dimensional array,
however, it does not work to save the values coming from the form to
calculate the price.
What's wrong???


There is no code there which does that. And if you already have an
array called $row why change it to an array called $articleRow It seems
identical apart from the variable $bildName which appears from nowhere.


Sorry, I forgot to change that variable name to english - it's
picname,
and yes basically they are identically -

Okay, that was stupid -

Anyway - All I want is an array that contains the data coming from the
databse
AND also the amount of each article coming from the form the user
fills.

I was able to make an array containing the amount of each article,
and I got the array coming from the database -
Now what I just don't like is that I don't have any evidence that
row[0] of the first array is identically to row[0] to my amount array
-
If I had all in one array, I could easily go through the array and
work with it - no matter if it was sorted or not.

Stefan
Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.