Javascript logo

In HTML tag 'input type = number' is not very user friendly. So, I always implement it as a text with transformation text to number.

To avoid the '0' in the beginning I use this snippet for onChange handling

    newValue =  (newValue.trim() === '' || parseInt(newValue.trim()) < 0) ? 0 : parseInt(newValue)

Then if '0' is filled and you type something, instead of the first 0 there will be your value.

Update 16.9.2024

In JS you can set the alternative value if false, for example:

var value = parseInt(e.target.value) || 0;

Then parsing the string with transformation to numeric is like this:

const newValue = (newValue.trim() === '' || parseInt(newValue.trim()) < 0 ) ? 0 : parseInt(newValue.value) || 0