chetrasho (OP)
Newbie
Offline
Activity: 25
Merit: 0
|
|
May 22, 2011, 05:32:55 PM |
|
Hi,
I'm writing a PHP script for bitcoins. I need a regex to check if a string is a valid number of bitcoins.
I've been trying stuff like [0-9]{0,8}\.[0-9]{0,8} but I know that's not really right. I suck at regexes...
Any help would be greatly appreciated...
Thanks TRSH0
|
|
|
|
|
|
|
|
The Bitcoin software, network, and concept is called "Bitcoin" with a capitalized "B". Bitcoin currency units are called "bitcoins" with a lowercase "b" -- this is often abbreviated BTC.
|
|
|
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
|
|
|
|
kseistrup
|
|
May 22, 2011, 05:38:32 PM |
|
Would it be easier to convert the amount to a float, then assert that the number is positive and <= 21E6?
Cheers,
|
Klaus Alexander Seistrup
|
|
|
chetrasho (OP)
Newbie
Offline
Activity: 25
Merit: 0
|
|
May 22, 2011, 05:54:26 PM |
|
Well, the variable $bitcoinamount is coming form post data $_POST['bitcoinamount']. So it's not necessarily a number to begin with.
I guess I could do something like
if ( is_float($ba) && $ba <= 21E6 && num_decimals($ba) <= 8 ) { return true; }
But I need a num_decimals() function.
|
|
|
|
kseistrup
|
|
May 22, 2011, 06:00:47 PM |
|
I don't speak PHP, but in Python you could do something like: def num_decimals(amount_string): (integer, decimals) = amount_string.split('.') return len(decimals)
Cheers,
|
Klaus Alexander Seistrup
|
|
|
bittrader
Jr. Member
Offline
Activity: 42
Merit: 1
|
|
May 22, 2011, 06:29:20 PM |
|
Something like this would probably work. $btc = $_POST['bitcoinamount'];
// Verify that the amount is (1) less than 21 million, (2) contains only numerals and an optional decimal point, and (3) contains a maximum of eight number after the decimal point. if (preg_match( '/^[0-9]*\.?[0-9]{0,8}$/', $btc) && $btc <= 21000000) echo "valid"; else echo "fail";
Depending what you're doing, you might want to also verify that the amount isn't zero.
|
|
|
|
chetrasho (OP)
Newbie
Offline
Activity: 25
Merit: 0
|
|
May 22, 2011, 06:40:11 PM |
|
Awesome. That's basically what I need, bittrader. Thank you so much!
|
|
|
|
theymos
Administrator
Legendary
Offline
Activity: 5194
Merit: 12968
|
|
May 22, 2011, 11:53:27 PM |
|
I think this would work as a pure-regex solution: ^0*(1?[0-9]|20)?[0-9]{0,6}(\.[0-9]{0,8}0*)?$
|
1NXYoJ5xU91Jp83XfVMHwwTUyZFK64BoAD
|
|
|
chetrasho (OP)
Newbie
Offline
Activity: 25
Merit: 0
|
|
May 23, 2011, 01:51:10 AM |
|
Nice. Thank you. Regexs are pretty sweet. It's just a few characters but over time I hope your work helps tons of developers...
|
|
|
|
nopara73
Member
Offline
Activity: 99
Merit: 326
|
|
April 01, 2016, 06:10:46 PM |
|
I think this would work as a pure-regex solution: ^0*(1?[0-9]|20)?[0-9]{0,6}(\.[0-9]{0,8}0*)?$ This is a good starting point, but it is not perfect. (I am not going to give the solution, but I want to note it before someone starts to use it blindly based on a quick google search.) For example it matches the input: 0000000000000000000000000000000009999999.00000000000000000000000000000000000000 0000000000000000 Also it doesn't let the "," to be used instead of ".", in some case you might want that that.
|
|
|
|
|