Code Snippets
Login
Reg national formatter
Controls the form of an input to only permit numbers and adds symbols where needed
Label
INPUT_NAME.addEventListener("input", function (event) { let value = event.target.value.replace(/\D/g, ""); // replace non digits with nothing let formatted = ""; // create the empty string // As digits are added, count them and take necessary action if (value.length > 0) formatted += value.substring(0, 2); if (value.length > 2) formatted += "." + value.substring(2, 4); if (value.length > 4) formatted += "." + value.substring(4, 6); if (value.length > 6) formatted += "-" + value.substring(6, 9); if (value.length > 9) formatted += "." + value.substring(9, 11); event.target.value = formatted; // change the input to the formatted string });
Copy to clipboard
Copied!
©
Leerlandais 2024