πŸ‘οΈCheck the validity of the address

In this section we'll check the validity of an EVM address using ethers

Check the validity of an EVM address using ethers.js

const { ethers } = require('ethers');

// Specify an Ethereum address to test.
const ethereumAddressToTest = 'testAddy';

// Function to check the validity of an Ethereum address.
const isValidEthereumAddress = (address) => {
  try {
    // Ensure the address is in a valid format.
    const validAddress = ethers.utils.getAddress(address);

    // Check if the formatted address is equal to the original address.
    if (validAddress.toLowerCase() === address.toLowerCase()) {
      return true;
    } else {
      return false;
    }
  } catch (error) {
    return false;
  }
};

// Call the function to check the validity of the specified Ethereum address.
const isValidEthAddress = isValidEthereumAddress(ethereumAddressToTest);

// Output the result.
console.log(`Is this Ethereum address valid? ${isValidEthAddress}`);

Last updated