<p>part of <a href="https://github.com/mikegogulski/bitcoin-php/blob/master/src/bitcoin.inc" rel="nofollow">https://github.com/mikegogulski/bitcoin-php/blob/master/src/bitcoin.inc</a></p>

<pre><code> private static $hexchars = "0123456789ABCDEF";
 private static $base58chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";

public static function checkAddress($addr, $addressversion = "00") // hex byte
{
    $addr = self::decodeBase58($addr);
    if (strlen($addr) != 50) {
        return false;
    }
    $version = substr($addr, 0, 2);
    if (hexdec($version) &gt; hexdec($addressversion)) {
        return false;
    }
    $check = substr($addr, 0, strlen($addr) - 8);
    $check = pack("H*", $check);
    $check = strtoupper(hash("sha256", hash("sha256", $check, true)));
    $check = substr($check, 0, 8);
    return $check == substr($addr, strlen($addr) - 8);
}


private function decodeBase58($base58)
{
    $origbase58 = $base58;

    //only valid chars allowed
    if (preg_match('/[^1-9A-HJ-NP-Za-km-z]/', $base58)) {
        return "";
    }

    $return = "0";
    for ($i = 0; $i &lt; strlen($base58); $i++) {
        $current = (string)strpos(self::$base58chars, $base58[$i]);
        $return = (string)bcmul($return, "58", 0);
        $return = (string)bcadd($return, $current, 0);
    }

    $return = self::encodeHex($return);

    //leading zeros
    for ($i = 0; $i &lt; strlen($origbase58) &amp;&amp; $origbase58[$i] == "1"; $i++) {
        $return = "00" . $return;
    }

    if (strlen($return) % 2 != 0) {
        $return = "0" . $return;
    }

    return $return;
}

private function encodeHex($dec)
{
    $return = "";
    while (bccomp($dec, 0) == 1) {
        $dv = (string)bcdiv($dec, "16", 0);
        $rem = (integer)bcmod($dec, "16");
        $dec = $dv;
        $return = $return . self::$hexchars[$rem];
    }
    return strrev($return);
}
</code></pre>

CC BY-SA 3.0
2013-04-18T23:18:48.050Z
2014-02-12T00:24:30.613Z
2014-02-12T00:24:30.613Z
Bitcoineer
<p>also, the official client bitcoind has a 'validateaddress' command. see <a href="https://en.bitcoin.it/wiki/Original_Bitcoin_client/API_Calls_list">https://en.bitcoin.it/wiki/Original_Bitcoin_client/API_Calls_list</a></p>

<p>you could call that from your code if you happen to have bitcoind running already.</p>

CC BY-SA 3.0
2012-01-13T16:21:44.930Z
I won't be running a bitcoind instance, but thanks regardless.
ripper234
CC BY-SA 3.0
2012-01-13T15:48:44.283Z
2012-01-13T15:48:44.283Z
nanotube
<p>For BitcoinJ (Java) it should be something like this:</p>

<pre><code>public boolean isValidAddress(String address) {
    try {
        new Address(params, address);
        return true;
    } catch(AddressFormatException e) {
        return false;
    }
}
</code></pre>

<p>API for address can be found here: <a href="https://bitcoinj.github.io/javadoc/0.11/com/google/bitcoin/core/Address.html" rel="nofollow">https://bitcoinj.github.io/javadoc/0.11/com/google/bitcoin/core/Address.html</a></p>

CC BY-SA 3.0
2014-07-28T01:52:24.137Z
2014-07-28T01:52:24.137Z
pm_labs
<p>For Javascript, you can use <a href="http://bitcore.io/" rel="nofollow">http://bitcore.io/</a> library. Here is an example:</p>

<p><a href="https://github.com/bitpay/bitcore/blob/master/examples/browser/example.html" rel="nofollow">https://github.com/bitpay/bitcore/blob/master/examples/browser/example.html</a></p>

CC BY-SA 3.0
2015-11-05T23:35:19.780Z
above link is dead
derrend
CC BY-SA 3.0
2014-10-06T10:40:34.640Z
2014-10-06T10:40:34.640Z
Jose Celano
<p>For Java, have a look at the validateAddress(String) method in this class:
<a href="https://github.com/jim618/multibit/blob/master/src/main/java/org/multibit/viewsystem/swing/action/Validator.java" rel="nofollow">https://github.com/jim618/multibit/blob/master/src/main/java/org/multibit/viewsystem/swing/action/Validator.java</a></p>

CC BY-SA 3.0
2012-01-13T10:04:35.490Z
2013-12-22T18:46:59.853Z
2013-12-22T18:46:59.853Z
jim618
<p>Is there a code snippet that validates a bitcoin address?</p>

<p>I'm looking for both javascript and java snippets. The code should support both testnet and real addresses.</p>

<development><address>
How to validate a Bitcoin address is a real one?
ripper234
# How to validate a Bitcoin address is a real one?

(Asked by: username_0 on 2012-01-13)

Is there a code snippet that validates a bitcoin address?

I'm looking for both javascript and java snippets. The code should support both testnet and real addresses.

**Tags:** development, address


---

## 5 answers

### Answer by username_6 on 2012-01-13. Score: 5 | Accepted answer

For Java, have a look at the validateAddress(String) method in this class:
<https://github.com/username_6/multibit/blob/master/src/main/java/org/multibit/viewsystem/swing/action/Validator.java>


---

### Answer by username_1 on 2013-04-18. Score: 9

part of <https://github.com/mikegogulski/bitcoin-php/blob/master/src/bitcoin.inc>

```
 private static $hexchars = "0123456789ABCDEF";
 private static $base58chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";

public static function checkAddress($addr, $addressversion = "00") // hex byte
{
    $addr = self::decodeBase58($addr);
    if (strlen($addr) != 50) {
        return false;
    }
    $version = substr($addr, 0, 2);
    if (hexdec($version) > hexdec($addressversion)) {
        return false;
    }
    $check = substr($addr, 0, strlen($addr) - 8);
    $check = pack("H*", $check);
    $check = strtoupper(hash("sha256", hash("sha256", $check, true)));
    $check = substr($check, 0, 8);
    return $check == substr($addr, strlen($addr) - 8);
}

private function decodeBase58($base58)
{
    $origbase58 = $base58;

    //only valid chars allowed
    if (preg_match('/[^1-9A-HJ-NP-Za-km-z]/', $base58)) {
        return "";
    }

    $return = "0";
    for ($i = 0; $i < strlen($base58); $i++) {
        $current = (string)strpos(self::$base58chars, $base58[$i]);
        $return = (string)bcmul($return, "58", 0);
        $return = (string)bcadd($return, $current, 0);
    }

    $return = self::encodeHex($return);

    //leading zeros
    for ($i = 0; $i < strlen($origbase58) && $origbase58[$i] == "1"; $i++) {
        $return = "00" . $return;
    }

    if (strlen($return) % 2 != 0) {
        $return = "0" . $return;
    }

    return $return;
}

private function encodeHex($dec)
{
    $return = "";
    while (bccomp($dec, 0) == 1) {
        $dv = (string)bcdiv($dec, "16", 0);
        $rem = (integer)bcmod($dec, "16");
        $dec = $dv;
        $return = $return . self::$hexchars[$rem];
    }
    return strrev($return);
}

```


---

### Answer by username_2 on 2012-01-13. Score: 9

also, the official client bitcoind has a 'validateaddress' command. see <https://en.bitcoin.it/wiki/Original_Bitcoin_client/API_Calls_list>

you could call that from your code if you happen to have bitcoind running already.

> **Comments:**
> **username_0**: I won't be running a bitcoind instance, but thanks regardless.  

---

### Answer by username_3 on 2014-07-28. Score: 2

For BitcoinJ (Java) it should be something like this:

```
public boolean isValidAddress(String address) {
    try {
        new Address(params, address);
        return true;
    } catch(AddressFormatException e) {
        return false;
    }
}

```

API for address can be found here: <https://bitcoinj.github.io/javadoc/0.11/com/google/bitcoin/core/Address.html>


---

### Answer by username_4 on 2014-10-06. Score: 1

For Javascript, you can use <http://bitcore.io/> library. Here is an example:

<https://github.com/bitpay/bitcore/blob/master/examples/browser/example.html>

> **Comments:**
> **username_5**: above link is dead  

---

