user78: 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.

user18939: For Javascript, you can use http://bitcore.io/ library. Here is an example:
https://github.com/bitpay/bitcore/blob/master/examples/browser/example.html

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

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

user441: 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.

user2892: 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);
}


78: <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>
 18939: <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>
 18591: <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>
 888: <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>
 441: <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>
 2892: <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>
