Please tell me, inputmask is not deleted. What could be the problem? The code has a condition if the country code from the array is equal to the value of input and the key with the code 8 is pressed, then the imputmask plugin is removed from the input. This is not happening now, because different country code values and input values in the script. In the input itself, you can see that the values are the same.
$(document).ready(function() { $("#phone").inputmask("+380 (99) 999-99-99"); }) var phones = [{ "country": "UA", "code": "+380", "mask": "(99) 999-99-99" }, { "country": "RU", "code": "+7", "mask": "(999) 999-99-99" }, { "country": "MD", "code": "+373", "mask": "(99) 999-99-99" } ]; var find_phone = 0; $("#phone").keydown(function(event) { var val = $(this).val(); val = val.replace(/[^+\\d]/g, ''); var phone_arr = phones.find(phones => val.includes(phones.code)); if (phone_arr != null && find_phone == 0) { find_phone = 1; $(this).inputmask(phone_arr.code + " " + phone_arr.mask); } if (phone_arr != null) { console.log(phone_arr.code); console.log(val); } else {} if (phone_arr != null && phone_arr.code == val && event.which == 8) { find_phone = 0; $(this).inputmask('remove'); $(this).val(""); } else {} });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/inputmask/4.0.9/jquery.inputmask.bundle.min.js"></script> <input type="text" id="phone" value="+380123456789">
inputmask
accepts multiple masks
https://github.com/RobinHerbots/Inputmask#keepstatic
var phones = [{ "country": "UA", "code": "+380", "mask": "(99) 999-99-99" }, { "country": "RU", "code": "+7", "mask": "(999) 999-99-99" }, { "country": "MD", "code": "+373", "mask": "(99) 999-99-99" } ]; $(document).ready(function() { $("#phone").inputmask({ mask: phones.map(phone => phone.code + ' ' + phone.mask) }); })
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/inputmask/4.0.9/jquery.inputmask.bundle.min.js"></script> <input type="text" id="phone" value="+380123456789">
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.