Hello. Tell me, please, how to make additions to the script correctly, so that in the second input field with id="din" only two numbers are allowed to enter, and after them three zeros are added through the space (as in the first field +7 is added)? Thank!
window.addEventListener("DOMContentLoaded", function() { function setCursorPosition(pos, elem) { elem.focus(); if (elem.setSelectionRange) elem.setSelectionRange(pos, pos); else if (elem.createTextRange) { var range = elem.createTextRange(); range.collapse(true); range.moveEnd("character", pos); range.moveStart("character", pos); range.select() } } function mask(event) { var matrix = "+7 (___) ___ ____", i = 0, def = matrix.replace(/\D/g, ""), val = this.value.replace(/\D/g, ""); if (def.length >= val.length) val = def; this.value = matrix.replace(/./g, function(a) { return /[_\d]/.test(a) && i < val.length ? val.charAt(i++) : i >= val.length ? "" : a }); if (event.type == "blur") { if (this.value.length == 2) this.value = "" } else setCursorPosition(this.value.length, this) }; var input = document.querySelector("#tel"); input.addEventListener("input", mask, false); input.addEventListener("focus", mask, false); input.addEventListener("blur", mask, false); }); <input type="text" name="" id="tel" class="f_input" /> <input type="text" name="" id="din" class="f_input" />
немного поразбираться с JavaScript самостоятельноon myнемного поразбираться с JavaScript самостоятельно, but to my great regret I really, for all my strong desire, will not find so much time for this, unlike yours. You also have time to answer my question by teaching me, but also in addition to follow my story. All the best. - LADYX