|
Posted by -Lost on 12/23/06 21:22
"Gleep" <Gleep@Gleep.com> wrote in message
news:2s1ro255i7dqsdniqt7qicbj3c3mr3kjhd@4ax.com...
> On Fri, 22 Dec 2006 13:00:03 -0800, "nostabo" <nostabo@microsoft.com> wrote:
>
>>Hi all,
>>
>>I have been trying to display numbers (actually money amounts) on an HTML
>>form in text type input boxes and then use the posted values in calculations
>>using PHP. The problem is that I can accept inputted values from a text box
>>when entered in the 1000 or 1000.00, but when a users adds a comma the value
>>(for example 1,000.00) the value is displayed as 1.0 by the number_format
>>function (see my code below)
>>
>><input type='text' id='software' name='software' value='<?php print
>>number_format($_POST['software'], 2);?>' >
>>
>>But, this also affects any reposting of the form since the posted var takes
>>on the value as displayed in the text box.
>>
>>Is there a way around this? This bad value eventually shows up in the
>>"posted" var if the form is resent after the user makes a change, so it can
>>screw up my calculations.
>>
>>TIA,
>>
>>Rick
>>
>
>
> What you want to do here is separate out formatting from the content, run or save your
> calculation
> then display it back with formatting.
>
> function clear_currency($cca){
> $ccb = array("$","," );
> $ccc = str_replace($ccb, "",$cca );
> return $ccc;
> }
>
> you get the submitted value of the form
>
> $someAmount = clear_currency( $_POST['dollarAmount'] );
>
> Note depending on your app you could also have some thing that removes the cents .00
> by using substring command
>
> list($dollars, $change) = split('.', $amount);
>
> also sometimes people decide to be wise asses and type in crazy stuff when you are
> expecting a
> number only that you could add this function...
>
> function numberOnly($no){
> $nno = ereg_replace ('[^0-9]+', '', $no);
> return $nno;
> }
>
>
> So now you know how to filter out a currency to a regular number, now run your calc,
> then you need
> to display it back you could use this
>
> <?= "\$".number_format($number, 2, '.', '') ?>
Great examples, Gleep!
I would however test my cleaned inputs with native functions though. For example,
replacing your numberOnly() function with ctype_digit(). Of course there is ctype_alnum()
for alphanumeric and ctype_alpha() for all alphabetical (for those who were not aware).
Be well and Happy Holidays.
-Lost
Navigation:
[Reply to this message]
|