How to Check if a Given String is a Valid Ethereum Wallet Address?


A Valid Ethereum Wallet Address Starts with “0x” and is 42 characters long. To check if a given string is a valid Ethereum address, you would typically:

  • Check if it’s 42 characters long (including the 0x prefix).
  • Ensure it consists only of hexadecimal characters.
  • Optionally, verify the checksum if it’s in mixed-case EIP-55 format.

Here’s how you can check for the validity of an Ethereum address using web3.js:

1
2
3
4
5
6
7
8
9
10
const Web3 = require('web3');
const web3 = new Web3();
 
const isValidEthereumAddress = (address) => {
    return web3.utils.isAddress(address);
};
 
// Example
const address = '0x...';  // Replace with your address
console.log(isValidEthereumAddress(address) ? 'Valid' : 'Invalid');
const Web3 = require('web3');
const web3 = new Web3();

const isValidEthereumAddress = (address) => {
    return web3.utils.isAddress(address);
};

// Example
const address = '0x...';  // Replace with your address
console.log(isValidEthereumAddress(address) ? 'Valid' : 'Invalid');

The web3.utils.isAddress() function checks the basic structure (i.e., length and hexadecimal format) of the address. If an address is mixed-case, it will also check its checksum based on EIP-55.

Note that just because an address is valid in format doesn’t mean it’s been used on the Ethereum network or that it has a corresponding private key. It simply means that it fits the expected structure of an Ethereum address.

Ethereum Blockchain

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
262 words
Last Post: How to Get the Latest Block Number (Head Block) on Ethereum Blockchain?
Next Post: How to Check the Balance of a Ethereum Wallet Address?

The Permanent URL is: How to Check if a Given String is a Valid Ethereum Wallet Address?

Leave a Reply