Author

Topic: Regex for bitcoin amounts? (Read 2201 times)

member
Activity: 103
Merit: 327
April 01, 2016, 01: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.
newbie
Activity: 25
Merit: 0
May 22, 2011, 08:51:10 PM
#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...
administrator
Activity: 5222
Merit: 13032
May 22, 2011, 06: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*)?$
newbie
Activity: 25
Merit: 0
May 22, 2011, 01:40:11 PM
#7

Awesome. That's basically what I need, bittrader. Thank you so much!
jr. member
Activity: 42
Merit: 1
May 22, 2011, 01: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.
hero member
Activity: 566
Merit: 500
Unselfish actions pay back better
May 22, 2011, 01: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,
newbie
Activity: 25
Merit: 0
May 22, 2011, 12: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.
hero member
Activity: 566
Merit: 500
Unselfish actions pay back better
May 22, 2011, 12: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,
newbie
Activity: 25
Merit: 0
May 22, 2011, 12: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)...
newbie
Activity: 25
Merit: 0
May 22, 2011, 12: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
Jump to: