Is there any way I can tell whether a transaction hash is valid or not?
For eg,

0x2592cf699903e83bfd664aa4e339388fd044fe31643a85037be803a5d162729f is valid transaction Hash whereas 0x62720366ef403c9891e2bfbd5358ee3c8a57b113 is not.

How to test programmatically whether a transaction hash is valid or not using web3?

I wish to have a function like:

isValidTransactionHash(String txHash){
// if transaction hash is valid return true else false

3 Answers 3


This method will only validate if the transaction hash has the right characters in the right length, it does not check the blockchain. Useful for string validation on user inputs.

function validate_txhash(addr)
  return /^0x([A-Fa-f0-9]{64})$/.test(addr);

This regex was supplied by this answer: https://ethereum.stackexchange.com/a/34286/50301


I would try with web3.eth.getTransactionReceipt inside a try-catch block:

try {
  var txR = web3.eth.getTransactionReceipt("0x62720366ef403c9891e2bfbd5358ee3c8a57b11");
  if (txR.blockNumber == undefined)
    throw "transaction receipt not found";
catch(e) {
  console.log("invalid tx receipt: " + e);
  • But getTransactionteceipt is not meant for that. It returns null for any string starting with '0x' followed by any string of length 64. Like 0x1111111111111111111111111111111111111111111111111111111111111111 and 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa all are valid transaction hashes according ti this but actually they are not. May 4, 2017 at 10:59
  • then simply check for some expected child, such as blockNumber. I added that to the example code.
    – SCBuergel
    May 4, 2017 at 11:09
  • That's not the way man. See, getTransactionReceipt returns null for both an incorrect transaction Hash as well as the transaction that is not mined yet. So checking this way will declare all transaction Hashes which are valid but not yet mined as invalid.Hope you understand. May 4, 2017 at 11:31
  • Did you test the code that I updated before posting my previous comment? I did and see it working. Please check again.
    – SCBuergel
    May 4, 2017 at 11:48
  • I tested that. If you pass a transactionHash that is not yet mined (but transaction hash is valid), the value of txR will be null, and since in next line when you try to access blockNumber of null, this will give an error. and in this case, a valid transaction has is also declared as invalid. Please check the code with a valid but unmined transaction. May 4, 2017 at 11:56
const search = async hash => {
    const re = /[0-9A-Fa-f]{6}/g;
    // 0x + 64 bytes
    if(hash && hash.length === 66 && re.test(hash)) {
      const item = await contract.methods.getItemByHash(hash).call()
  • The regex doesn't seem correct.
    – Ismael
    Dec 27, 2021 at 4:07
  • @Ismael why? I do not see any issue
    – Yilmaz
    Dec 27, 2021 at 4:10
  • Sorry, my mistake. I didn't test it correctly.
    – Ismael
    Dec 28, 2021 at 5:30
  • 1
    @Ismael I do not accept plain sorry :) just upvote the answer :)
    – Yilmaz
    Dec 28, 2021 at 5:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.