JSFiddle - React, Tailwind, and code Playground
by Roko
HTML
JS<input type="text" class="form-control js_leading_zero" placeholder="000000" maxlength="6">
JavaScript
const inputField = document.querySelector('.js_leading_zero')
inputField.addEventListener('blur', updateInput);
inputField.addEventListener('focus', clearInput);
function updateInput() {
if (inputField.value.length) {
inputField.value = inputField.value.padStart(6, '0');
inputField.placeholder = inputField.value;
} else {
inputField.value = inputField.placeholder;
}
}
function clearInput() {
inputField.value = "";
}