Does web3.js have a function that checks if a value (Number | String | BigNumber) is a valid Ether Value? If not, what might be an efficient way of doing so? Would I have to manually make a function to make sure that the input value doesn't have more than 18 decimals, isn't negative, etc?


My application is using web3.js with geth and users will be able to withdraw funds from their balances at any time. Now during the withdraw process, they input the amount they wish to withdraw. To my understanding, These are the characteristics of Ether:

  • 18 decimal points
  • Greater or equal to zero

I could make a function that measures how many decimal points exist in the users input, but would I be reinventing the wheel?

1 Answer 1


More specifically, you would want to check that the amount of Ether sent along plus gas limit * gas price is less or equal to the current balance of that account, this should include all other tests. In web3 you could do this via:

var sender = 0x123...;
var to = 0xabc...;
var value = 123; // Wei
var gaslimit = 100000;
var gasprice = 20000000000; // 20 GWei per gas

if (web3.eth.getBalance(sender) >= value + gaslimit * gasprice)
  web3.eth.sendTransaction({from: sender, to: to, gas: gaslimit, gasprice: gasprice, value: value});
  • Oh the gas calculations I already have :). The issue I'm having is with the input and making sure it's a valid Ether value. Once I know it's a valid Ether value, I can do the calculation you have above :)
    – Webeng
    Jul 16, 2017 at 9:10
  • IMO you just have to check for overflow before the if statement. The maximally minted number of Ether is unknown (in contrast to Bitcoin), hence it will be hard to check for a "correct" input number.
    – SCBuergel
    Jul 16, 2017 at 9:25
  • oh yeah for sure, and what about the decimal values? I already use the BigNumber.js library, and check if it's >0 and a valid number. Now if it has 19 decimal places, it shouldn't be valid. This is the main issue I am having. Wouldn't want to code something half right and weird stuff happen at strange times.
    – Webeng
    Jul 16, 2017 at 9:30
  • treat all in Wei and dont take decimal values.
    – SCBuergel
    Jul 16, 2017 at 10:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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