Welcome Guest, Not a member yet? Register   Sign In
How do I validate this?
#1

[eluser]RS71[/eluser]
Hello,

I have two inputs available so that the user can define a monetary range. What I'd like to do is make sure that nothing harmful is being input but also, make the number usable. What I mean by this is, lets say the user inputs "$100,000.00", I have to transform it to "100000" so I can use it in a DB (in my region, commas are used for cents so I believe I have to remove it)

Can I achieve this?

Thanks in advance,
RS71
#2

[eluser]pistolPete[/eluser]
Have a look at these PHP functions:

- money_format()
- number_format()
#3

[eluser]meade[/eluser]
I would convert in [removed]
var currency = "$1,100.00";
var number = Number(currency.replace(/[^0-9\.]+/g,""));
(http://stackoverflow.com/questions/55911...javascript)

and then confirm the # is in range prior to saving to the DB. This allows user feedback without a server call AND checking on the backend ensures no one is 'playing' and sending invalid information.
#4

[eluser]RS71[/eluser]
Thank you both for your replies.

Meade, I like your approach but the only problem I'm having is it doesn't seem to be removing the period so the number function ends up making 1.000 (a thousand) into 1. What do you suggest?
#5

[eluser]RS71[/eluser]
Ended up doing:

Code:
function nFormat(nStr) {
            nStr = nStr.split(',');
            nStr = nStr[0];
            var number = Number(nStr.replace(/[^0-9]+/g,""));
            return number;
        }

I put in a part to remove cents as well. Thanks guys.




Theme © iAndrew 2016 - Forum software by © MyBB