Making Input field for money value

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
ninethousandfeet
Forum Contributor
Posts: 130
Joined: Tue Mar 10, 2009 4:56 pm

Making Input field for money value

Post 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!';
}
 
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Making Input field for money value

Post 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 .
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
ninethousandfeet
Forum Contributor
Posts: 130
Joined: Tue Mar 10, 2009 4:56 pm

Re: Making Input field for money value

Post 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!
ninethousandfeet
Forum Contributor
Posts: 130
Joined: Tue Mar 10, 2009 4:56 pm

Re: Making Input field for money value

Post 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?
Post Reply