Page 1 of 1

Making Input field for money value

Posted: Sat Apr 04, 2009 7:45 pm
by ninethousandfeet
hello,

i am trying to make my input field for a monetary value fixed... i would like it to be something like:
$[blank text box here].[blank for cents]... i want the '$' and '.' to be fixed... does anyone know how i can do this? also, any suggestions on errors i should set when users are entering money values. i was going to do something like:

Code: Select all

 
$number = "0-9";
if (!$number) {
$error[]= 'Value must be a number!';
}
 

Re: Making Input field for money value

Posted: Sat Apr 04, 2009 7:51 pm
by s.dot
The most logical solution would be to provide two input boxes. One for the dollars and one for the cents. The cents could even be a select box (from 00-99).

If you only want one input box you could explode on the "."

Code: Select all

$pieces = explode('.', $_POST['money']);
if (count($pieces) == 2)
{
    $dollars = $pieces[0];
    $cents = $pieces[1];
} elseif (count($pieces) == 1)
{
    $dollars = $pieces[0];
    $cents = 00;
} else
{
    //error
}
Things you should be checking for..

* the value is not empty
* the value is numeric
* the cents part is between 00-99
* some places use a , instead of a .

Re: Making Input field for money value

Posted: Sat Apr 04, 2009 9:01 pm
by ninethousandfeet
okay so i took your advice and created two fields, one for the dollar amount and one for the cents... should i just make both of them int or should i set them up as decimal type in my db? i use mysql...
also, i made the following error message for the cents and i'm unable to input any two digit value from 00-99?? any ideas why? what is wrong with my error conditional statement?

Code: Select all

 
if (!ereg("^[00-99]$", $_POST['amount_cents'])) {
      $error['money_cents'] = 'Price cents field should be in 2-digit format. Example: Type 25 for 25 cents.';
  }
 
thank you!

Re: Making Input field for money value

Posted: Sat Apr 04, 2009 10:27 pm
by ninethousandfeet
if i create an extra field for the cents, then there is an extra field in my db, right? if so, then how do i link the two fields together to submit the price with my paypal 'buy now' form? any ideas?