Validating input as money value??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
angelena
Forum Commoner
Posts: 53
Joined: Mon Nov 22, 2004 4:10 am

Validating input as money value??

Post by angelena »

I have an input field for member input the money value,but i cant control the member either input the value as $12 or $12.12....

Let say if the field name is $money and i set that as a currency using setlocale ....will that seems logic?

Thanks in advance .. :(
User avatar
Crom
Forum Newbie
Posts: 8
Joined: Mon Nov 08, 2004 2:50 am
Location: Ukraine
Contact:

Post by Crom »

Use regular expressions. There are lots of them for money validation. Type in google 'regular expressions'.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Validating input as money value??

Post by pickle »

angelena wrote:I have an input field for member input the money value,but i cant control the member either input the value as $12 or $12.12....
Actually, you could if you used Javascript - just do a check after each key pressed, with rules determining if it's a valid character.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

You should find some way of validating it on the server side agian but on the client side you can format the dollar value in your javascript by.
doesn't deal with $ or £ or € or any other money symbol but deals with number formating.

Code: Select all

money = money.toFixed(2);
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Something to remember: Don't prevent the user from entering a $. I've seen too many sites that do something like:

Do not enter a $, or don't enter - when entering your credit card. You're the programmer. Simply remove these common characters, and then validate the result from there.

So, if someone enters ` $29.95 `, which has an extra space at the beginning and end, and a dollar sign, you can easily strip out the extra spaces and the dollar sign. Then, validate the result, which would be 29.95. That's much more intelligent than simply outputting "Error!"
angelena
Forum Commoner
Posts: 53
Joined: Mon Nov 22, 2004 4:10 am

Post by angelena »

Ohhh....thx for helping me...i able to get it done :)
angelena
Forum Commoner
Posts: 53
Joined: Mon Nov 22, 2004 4:10 am

Post by angelena »

Ohhh....thx for helping me...i able to get it done :)
Post Reply