The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
Page 2 -
Price in form-input with format 0000.00
Page 2 - Discuss Price in form-input with format 0000.00 in the PHP Development forum on Dev Shed. Price in form-input with format 0000.00 PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 17th, 2013, 12:35 AM
|
 |
Code Monkey V. 0.9
|
|
Join Date: Mar 2005
Location: A Land Down Under
|
|
What you need to do is store the value in the database in a DECIMAL column. When a user enters the number, check it with is_numeric() to see if it's a numeric value or not. You can even use floatval() to convert the value to a number. When you display it, use number_format() and it will display the number just how you want it to.
|

January 17th, 2013, 01:12 AM
|
|
Contributing User
|
|
Join Date: Jan 2013
Posts: 40
Time spent in forums: 9 h 36 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by drumn4life0789 I guess I don't understand what is really going on here.
Seems to me that all that needs to be done is strip out anything that is not a number or a decimal. Then check to make sure that there is only one decimal in the string. Then you check that if there is a decimal that there is only two digits behind it.
if we get all that as true we have a valid input. But since it is money I would say you throw it back to the user and ask if this is correct. And format it like so in the display, so that it is easy for the user to read
$###,###.## |
Yes, but cant find how to do.
This does it but leaves the dot also for thousand, this gives me 1.100.50 when I need 1000.50
$price = '1.010.10';
$price = preg_replace("/[^0-9\.]/", "",$price);
and the filters, think arent using ok, maybe this is what I need however I doesnīt do it correctly:
$var = filter_var($price,FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
echo $var;
Thanks
|

January 17th, 2013, 03:45 AM
|
|
Contributing User
|
|
Join Date: Jan 2013
Posts: 40
Time spent in forums: 9 h 36 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by Catacaustic What you need to do is store the value in the database in a DECIMAL column. When a user enters the number, check it with is_numeric() to see if it's a numeric value or not. You can even use floatval() to convert the value to a number. When you display it, use number_format() and it will display the number just how you want it to. |
I am trying, but this and everything I do for 1.010.00 returns 1.01
as 1.010 is one number as gW said, so as there are 2 numbers in 1.010.00 what should I do, split them, delete the dot.
I am lost, as they may fill in the form many ways 1.000.10 and 1000.10 should end both 1000.10 and 1000 should end 1000.00
The only dot acceptable is a dot for decimals, and if none a .00 should be added.
|

January 17th, 2013, 07:07 AM
|
|
|
|
I am not a regex expert (ask on the regex forum) but I'm sure there is an expression that can strip out all non-numerics except the last decimal, if it exists.
__________________
There are 10 kinds of people in the world. Those that understand binary and those that don't.
|

January 17th, 2013, 07:25 AM
|
|
Contributing User
|
|
Join Date: Jan 2013
Posts: 40
Time spent in forums: 9 h 36 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by gw1500se I am not a regex expert (ask on the regex forum) but I'm sure there is an expression that can strip out all non-numerics except the last decimal, if it exists. |
thanks will do that, didnīt know there was a special forum for that.
|

January 18th, 2013, 08:59 AM
|
|
Contributing User
|
|
Join Date: Jan 2013
Posts: 40
Time spent in forums: 9 h 36 sec
Reputation Power: 1
|
|
Doing this I manage to convert the empty value for field decimal to 0, filling in the form field price 2 and field decimal leave it emtpy the echo echo $holidaycost; before the ctype_digit check prints correctly 2.0 but after the ctype_digit check it prints the value empty....
I need to validate the field decimal.
PHP Code:
$price = htmlspecialchars($_POST['price']);
$decimal = htmlspecialchars($_POST['decimal']);;
if (empty($decimal)) {
$decimal ='0';
$holidaycost = $price.".".$decimal;
echo $holidaycost;
echo "<br>";
}
if ( ctype_digit($_POST['price'])&& ctype_digit($_POST['decimal'])) {
$holidaycost = $price.".".$decimal;
echo "test $holidaycost";
}
|

January 18th, 2013, 10:04 AM
|
|
|
PHP Code:
$holidaycost = $_POST['price'].".".$_POST['decimal'];
|

January 18th, 2013, 11:52 AM
|
|
Contributing User
|
|
Join Date: Jan 2013
Posts: 40
Time spent in forums: 9 h 36 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by gw1500se
PHP Code:
$holidaycost = $_POST['price'].".".$_POST['decimal'];
|
Thanks, it didnīt work, but this did:
PHP Code:
if ( ctype_digit($_POST['price'])&& ctype_digit($decimal)) {
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|