How to validate a Bitcoin address is a real one? 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.
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.