Updates (June 2023)
web3.utils.isAddress
will soon be deprecated. Please use web3-validator
package instead. Example:
import { isAddress } from 'web3-validator';
isAddress('blah');
Using a Library (June 2016)
Libraries like web3.js and ethers have isAddress()
.
Examples:
The following is an answer from 2016.
Regular Address
EIP 55 added a "capitals-based checksum" which was implemented by Geth by May 2016.
Here's Javascript code from Geth:
/**
* Checks if the given string is an address
*
* @method isAddress
* @param {String} address the given HEX adress
* @return {Boolean}
*/
var isAddress = function (address) {
if (!/^(0x)?[0-9a-f]{40}$/i.test(address)) {
// check if it has the basic requirements of an address
return false;
} else if (/^(0x)?[0-9a-f]{40}$/.test(address) || /^(0x)?[0-9A-F]{40}$/.test(address)) {
// If it's all small caps or all all caps, return true
return true;
} else {
// Otherwise check each case
return isChecksumAddress(address);
}
};
/**
* Checks if the given string is a checksummed address
*
* @method isChecksumAddress
* @param {String} address the given HEX adress
* @return {Boolean}
*/
var isChecksumAddress = function (address) {
// Check each case
address = address.replace('0x','');
var addressHash = sha3(address.toLowerCase());
for (var i = 0; i < 40; i++ ) {
// the nth letter should be uppercase if the nth digit of casemap is 1
if ((parseInt(addressHash[i], 16) > 7 && address[i].toUpperCase() !== address[i]) || (parseInt(addressHash[i], 16) <= 7 && address[i].toLowerCase() !== address[i])) {
return false;
}
}
return true;
};
ICAP Address
ICAP has a checksum which can be verified. You can review Geth's icap.go and here's a snippet from it:
// https://en.wikipedia.org/wiki/International_Bank_Account_Number#Validating_the_IBAN
func validCheckSum(s string) error {
s = join(s[4:], s[:4])
expanded, err := iso13616Expand(s)
if err != nil {
return err
}
checkSumNum, _ := new(big.Int).SetString(expanded, 10)
if checkSumNum.Mod(checkSumNum, Big97).Cmp(Big1) != 0 {
return ICAPChecksumError
}
return nil
}