[Ethereum] How to test if a TransactionHash is valid using web3


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

Best Answer

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

Related Topic