Bitcoin Forum
November 02, 2024, 08:35:34 PM *
News: Latest Bitcoin Core release: 28.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Regex for bitcoin amounts?  (Read 2197 times)
chetrasho (OP)
Newbie
*
Offline Offline

Activity: 25
Merit: 0


View Profile
May 22, 2011, 05:32:55 PM
 #1


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
chetrasho (OP)
Newbie
*
Offline Offline

Activity: 25
Merit: 0


View Profile
May 22, 2011, 05:35:44 PM
 #2


I found this thread: http://forum.bitcoin.org/index.php?topic=2459.msg33802#msg33802

But I'm still sorta lost on how to implement stuff in PHP. I just wanna do preg( $bitcoinregex, $bitcoinamount)...
kseistrup
Hero Member
*****
Offline Offline

Activity: 566
Merit: 500


Unselfish actions pay back better


View Profile WWW
May 22, 2011, 05:38:32 PM
 #3

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 Offline

Activity: 25
Merit: 0


View Profile
May 22, 2011, 05:54:26 PM
 #4


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
Hero Member
*****
Offline Offline

Activity: 566
Merit: 500


Unselfish actions pay back better


View Profile WWW
May 22, 2011, 06:00:47 PM
 #5

I don't speak PHP, but in Python you could do something like:

Code:
def num_decimals(amount_string):
    (integer, decimals) = amount_string.split('.')
    return len(decimals)

Cheers,

Klaus Alexander Seistrup
bittrader
Jr. Member
*
Offline Offline

Activity: 42
Merit: 1



View Profile
May 22, 2011, 06:29:20 PM
Merited by ABCbits (1)
 #6

Something like this would probably work.

Code:
$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 Offline

Activity: 25
Merit: 0


View Profile
May 22, 2011, 06:40:11 PM
 #7


Awesome. That's basically what I need, bittrader. Thank you so much!
theymos
Administrator
Legendary
*
Offline Offline

Activity: 5376
Merit: 13357


View Profile
May 22, 2011, 11:53:27 PM
 #8

I think this would work as a pure-regex solution:
Code:
^0*(1?[0-9]|20)?[0-9]{0,6}(\.[0-9]{0,8}0*)?$

1NXYoJ5xU91Jp83XfVMHwwTUyZFK64BoAD
chetrasho (OP)
Newbie
*
Offline Offline

Activity: 25
Merit: 0


View Profile
May 23, 2011, 01:51:10 AM
 #9



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 Offline

Activity: 103
Merit: 327


View Profile
April 01, 2016, 06:10:46 PM
Merited by ABCbits (2)
 #10

I think this would work as a pure-regex solution:
Code:
^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.

Creator of Wasabi Wallet: An open-source, non-custodial, privacy focused Bitcoin wallet - https://wasabiwallet.io
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!