JasperOL commited on
Commit
daaa022
·
verified ·
1 Parent(s): 6c21ae5

It needs to be able to actually write the number in the box

Browse files
Files changed (1) hide show
  1. script.js +53 -45
script.js CHANGED
@@ -9,57 +9,65 @@ document.addEventListener('DOMContentLoaded', function() {
9
  const activateBtn = card.querySelector('.agent-activate-btn');
10
  // Format phone number input
11
  phoneInput.addEventListener('keydown', function(e) {
12
- // Allow navigation keys
13
- if ([8, 9, 13, 37, 38, 39, 40].includes(e.keyCode)) {
14
  return;
15
  }
16
-
17
- // Allow only numbers and +
18
- if (!/[\d+]/.test(e.key)) {
19
- e.preventDefault();
 
 
 
 
20
  return;
21
  }
 
 
 
 
 
22
  });
23
-
24
- phoneInput.addEventListener('input', function(e) {
25
- let input = e.target;
26
- let startPos = input.selectionStart;
27
- let endPos = input.selectionEnd;
28
-
29
- // Get current value and cursor position
30
- let value = input.value;
31
-
32
- // Remove all non-digit characters
33
- let cleaned = value.replace(/\D/g, '');
34
-
35
- // Format based on cleaned value
36
- let formatted = '+64';
37
- if (cleaned.length > 2) {
38
- formatted += ` ${cleaned.substring(2, 5)}`;
39
- }
40
- if (cleaned.length > 5) {
41
- formatted += ` ${cleaned.substring(5, 8)}`;
42
- }
43
- if (cleaned.length > 8) {
44
- formatted += ` ${cleaned.substring(8, 12)}`;
45
- }
46
-
47
- // Update the input value
48
- input.value = formatted;
49
-
50
- // Calculate new cursor position
51
- let newCursorPos = startPos;
52
- if (value.length < formatted.length) {
53
- // If we added formatting characters, adjust cursor position
54
- const addedChars = (formatted.match(/ /g) || []).length - (value.match(/ /g) || []).length;
55
- if (startPos > 4 && addedChars > 0) {
56
- newCursorPos += addedChars;
57
  }
58
- }
59
-
60
- // Restore cursor position
61
- input.setSelectionRange(newCursorPos, newCursorPos);
62
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  // Toggle form on card click
64
  card.addEventListener('click', function(e) {
65
  // Don't toggle if clicking on form elements
 
9
  const activateBtn = card.querySelector('.agent-activate-btn');
10
  // Format phone number input
11
  phoneInput.addEventListener('keydown', function(e) {
12
+ // Allow: backspace, delete, tab, escape, enter, arrows
13
+ if ([46, 8, 9, 27, 13, 37, 38, 39, 40].includes(e.keyCode)) {
14
  return;
15
  }
16
+
17
+ // Allow: Ctrl+A, Ctrl+C, Ctrl+V, Ctrl+X
18
+ if ((e.ctrlKey || e.metaKey) && [65, 67, 86, 88].includes(e.keyCode)) {
19
+ return;
20
+ }
21
+
22
+ // Allow: home, end
23
+ if (e.keyCode >= 35 && e.keyCode <= 36) {
24
  return;
25
  }
26
+
27
+ // Ensure that it is a number and stop the keypress if not
28
+ if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
29
+ e.preventDefault();
30
+ }
31
  });
32
+ phoneInput.addEventListener('input', function(e) {
33
+ let input = e.target;
34
+ let startPos = input.selectionStart;
35
+
36
+ // Get current value and remove any existing formatting
37
+ let value = input.value.replace(/\D/g, '');
38
+
39
+ // Always start with +64
40
+ let formattedValue = '+64';
41
+
42
+ // Add formatting based on input length
43
+ if (value.length > 2) {
44
+ formattedValue += ' ' + value.substring(2, 5);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  }
46
+ if (value.length > 5) {
47
+ formattedValue += ' ' + value.substring(5, 8);
48
+ }
49
+ if (value.length > 8) {
50
+ formattedValue += ' ' + value.substring(8, 12);
51
+ }
52
+
53
+ // Update the input value
54
+ input.value = formattedValue;
55
+
56
+ // Calculate new cursor position
57
+ let newCursorPos = startPos;
58
+
59
+ // If we added formatting characters after the cursor position
60
+ // We need to move the cursor forward accordingly
61
+ if (value.length > 2 && startPos >= 4) newCursorPos++;
62
+ if (value.length > 5 && startPos >= 8) newCursorPos++;
63
+ if (value.length > 8 && startPos >= 12) newCursorPos++;
64
+
65
+ // Ensure cursor doesn't go beyond the input length
66
+ newCursorPos = Math.min(newCursorPos, formattedValue.length);
67
+
68
+ // Restore cursor position
69
+ input.setSelectionRange(newCursorPos, newCursorPos);
70
+ });
71
  // Toggle form on card click
72
  card.addEventListener('click', function(e) {
73
  // Don't toggle if clicking on form elements