Forum Moderators: coopster

Message Too Old, No Replies

How to check for a monetary value?

is numeric help on entering money values

         

adammc

4:58 am on Nov 22, 2006 (gmt 0)

10+ Year Member



Hi folks,

I have an input field in my form that inserts data into the database.

"Postage cost"

I need the user to only be able to enter dollar amounts in the form of 0.00, 1.50 etc

I do not want them to be able to enter $ signs and or text, only the dollar amounts.

I am currently using this code but it wont let you put in '0' as '0.00'.

[PHP]// Check that the postage field has been filled in.

if (empty ($postage)) {
$errors[] = 'Please enter a postage / delivery amount.<br />';
}
[/PHP]

[PHP]// check that the postage is in the correct format - no dollar signs
if (!is_numeric($postage)) {

$errors[] = 'Please input the postage in the form 123.45, without any other symbols.<br/>If postage is free, please enter 0.00<br><br>';
}[/PHP]

The above code is something I quicky added in, but my client wants to allow the users to enter '0' or leave it blank which in those cases would mean that $postage = 0.00

Any advice would be greatly appreciated.

Mohamed

1:00 pm on Nov 22, 2006 (gmt 0)

10+ Year Member



I hope this code will help you

$postage =0.00;
if(isset($postage)){
if($postage=="0.00")
echo "do something";
elseif (!is_numeric($postage))
echo "error message";
else
//do tha thing
echo"carry out the task";
}

henry0

2:49 pm on Nov 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Could you set a default 0.00

justgowithit

4:36 pm on Nov 22, 2006 (gmt 0)

10+ Year Member



In this case I would use regexp to validate instead of is_numeric. That way you don't have to worry about the float, the zero value, or a blank field. How about something like this:

if (isset($postage) && ereg('^[0,9]{1,3}(\.)[0-9]{1,2}$', $postage)){

//-> It's good go process (or set back to $postage)

}elseif (!isset($postage)){

//-> Postage is blank set to 0.00
$postage = '0.00';

}else{

//-> There's a problem set error
$errors[] = 'Please input the postage in the form 123.45, without any other symbols.<br/>If postage is free, please enter 0.00<br><br>';
}

coopster

4:01 pm on Nov 27, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I often use JavaScript on the client-side to limit keyboard presses of numbers and decimal points only. I then edit the value, if present, using JavaScript with the same regular expression as I use on the server-side and if we don't have a valid entry, alert the user. Saves a round trip to the server just to edit the numeric entry, that's all.

On both the client side and the server side (in case the user has JavaScript disabled) you can first strip any dollar signs and commas before running the value against your regex.

The only difference that I show here, justgowithit, is that I also make the decimal point and decimal place values optional.

if ($postage &&!preg_match("/^\d{0,9}(\.\d{0,2})?$/", $postage)) { 
$errors[] = 'The postage field accepts monetary values only.';
} else {
$postage += 0; // convert to numeric value of zero
}