bongzilla

Untitled

Mar 3rd, 2021 (edited)
74
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script>
  2. window.addEventListener("DOMContentLoaded", function() {
  3. [].forEach.call( document.querySelectorAll("input[type='tel']"), function(input) {
  4. var keyCode;
  5. function mask(event) {
  6. event.keyCode && (keyCode = event.keyCode);
  7. var pos = this.selectionStart;
  8. if (pos < 3) event.preventDefault();
  9. if (pos === 3 && event.keyCode === 56) {
  10. event.preventDefault();
  11. }
  12.  
  13. /*
  14. Берем значение инпута,
  15. удаляем все символы кроме цифр,
  16. если первая цифра 8, меняем ее на 7
  17. */
  18. var val = this.value.replace(/\D/g, "");
  19. if (val[0] === '8') val = '' + val.slice(1);
  20.  
  21. var matrix = "+7 (___) ___ ____",
  22. i = 0,
  23. def = matrix.replace(/\D/g, ""),
  24.  
  25. new_value = matrix.replace(/[_\d]/g, function(a) {
  26. return i < val.length ? val.charAt(i++) || def.charAt(i) : a
  27. });
  28. i = new_value.indexOf("_");
  29. if (i != -1) {
  30. i < 5 && (i = 3);
  31. new_value = new_value.slice(0, i)
  32. }
  33. var reg = matrix.substr(0, this.value.length).replace(/_+/g,
  34. function(a) {
  35. return "\\d{1," + a.length + "}"
  36. }).replace(/[+()]/g, "\\$&");
  37. reg = new RegExp("^" + reg + "$");
  38. if (!reg.test(this.value) || this.value.length < 5 || keyCode > 47 && keyCode < 58) this.value = new_value;
  39. if (event.type == "blur" && this.value.length < 17) {
  40. this.value = "";
  41. }
  42. }
  43.  
  44. input.addEventListener("input", mask, false);
  45. input.addEventListener("focus", mask, false);
  46. input.addEventListener("blur", mask, false);
  47. input.addEventListener("keydown", mask, false)
  48.  
  49. });
  50. });
  51.  
  52. $( document ).ready(function() {
  53. let fields = document.querySelectorAll("input[type='tel']");
  54.  
  55. for (let i = 0; i < fields.length; i++) {
  56. fields[i].addEventListener("paste", function(e) {
  57. var pastedText = undefined;
  58.  
  59. if (window.clipboardData && window.clipboardData.getData) {
  60. pastedText = window.clipboardData.getData('Text');
  61. } else {
  62. var clipboardData = (e.originalEvent || e).clipboardData;
  63. if (clipboardData && clipboardData.getData) {
  64. pastedText = clipboardData.getData('text/plain');
  65. }
  66.  
  67. if(pastedText[0] === "8") {
  68. e.target.value = "+7 " + pastedText.split("").slice(1).join("")
  69. } else {
  70. e.target.value = pastedText
  71. }
  72.  
  73. e.preventDefault();
  74. return false;
  75. }
  76. });
  77. }
  78. });
  79. </script>
RAW Paste Data