开发者

Converting an input text value to a decimal number

开发者 https://www.devze.com 2022-12-28 11:35 出处:网络
I\'m trying to work with decimal data in my PHP and MySql practice and I\'m not sure about how can I do for an acceptable level af accuracy.

I'm trying to work with decimal data in my PHP and MySql practice and I'm not sure about how can I do for an acceptable level af accuracy.

I've 开发者_如何学运维wrote a simple function which recives my input text value and converts it to a decimal number ready to be stored in the database.

<?php

function unit ($value, $decimal_point = 2) {
    return number_format (str_replace (",", ".", strip_tags (trim ($value))), $decimal_point);
}

?>

I've resolved something like AbdlBsF5%?nl with some jQuery code for replace and some regex to keep only numbers, dots and commas.

In some country, people uses the comma , to send decimal numbers, so a number like 72.08 is wrote like 72,08. I'd like avoid to forcing people to change their usual chars and I've decided to use a jQuery to keep this too.

Now every web developer knows the last validation must be handled by the dynamic page for security reasons.

So my answer is should I use something like unit (); function to store data or should I also check if users inserts invalid chars like letters or something else? If I try this and send letters, the query works without save the invalid data, I think this isn't bad, but I could easily be wrong because I'm a rookie.

What kind of method should I use for my query if I want a number like 99999.99?


don't forget to consider formatting also DATABASE values with something like FLOAT(10,2)

IMHO this is also very important!

then, of course use server side language for make real validation is the best practice!

you can read this:

http://php.net/manual/en/function.number-format.php

javascript can be fancy and handy but not secure although!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号