Bitcoin Forum
March 30, 2017, 12:59:15 PM *
News: Latest stable version of Bitcoin Core: 0.14.0  [Torrent]. (New!)
 
   Home   Help Search Donate Login Register  
Pages: [1]
  Print  
Author Topic: Regex for bitcoin amounts?  (Read 1818 times)
chetrasho
Newbie
*
Offline Offline

Activity: 25


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

Posts: 1490878755

View Profile Personal Message (Offline)

Ignore
1490878755
Reply with quote  #2

1490878755
Report to moderator
Even if you use Bitcoin through Tor, the way transactions are handled by the network makes anonymity difficult to achieve. Do not expect your transactions to be anonymous unless you really know what you're doing.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
1490878755
Hero Member
*
Offline Offline

Posts: 1490878755

View Profile Personal Message (Offline)

Ignore
1490878755
Reply with quote  #2

1490878755
Report to moderator
1490878755
Hero Member
*
Offline Offline

Posts: 1490878755

View Profile Personal Message (Offline)

Ignore
1490878755
Reply with quote  #2

1490878755
Report to moderator
1490878755
Hero Member
*
Offline Offline

Posts: 1490878755

View Profile Personal Message (Offline)

Ignore
1490878755
Reply with quote  #2

1490878755
Report to moderator
chetrasho
Newbie
*
Offline Offline

Activity: 25


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: 565


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
http://about.me/kseistrup
chetrasho
Newbie
*
Offline Offline

Activity: 25


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: 565


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
http://about.me/kseistrup
bittrader
Jr. Member
*
Offline Offline

Activity: 42



View Profile
May 22, 2011, 06:29:20 PM
 #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
Newbie
*
Offline Offline

Activity: 25


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
*
expert
Offline Offline

Activity: 2618


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
Newbie
*
Offline Offline

Activity: 25


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
Newbie
*
Offline Offline

Activity: 15


View Profile
April 01, 2016, 06:10:46 PM
 #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.
Pages: [1]
  Print  
 
Jump to:  

Sponsored by , a Bitcoin-accepting VPN.
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!