-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom-phone-field.js
45 lines (39 loc) · 1.22 KB
/
custom-phone-field.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
document.addEventListener("DOMContentLoaded", function () {
const phoneInputs = document.querySelectorAll(
'.jet-form-builder__field[name="phone"]'
);
phoneInputs.forEach((input) => {
const iti = window.intlTelInput(input, {
utilsScript:
"https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.13/js/utils.js",
preferredCountries: ["UK"], //set default country
//find more options in the library documentation
separateDialCode: true,
formatOnDisplay: true,
initialCountry: "auto",
// geoIpLookup: function (callback) {
// callback("ES");
// },
});
//validation customization
const form = input.closest("form");
if (form) {
form.addEventListener("submit", function (e) {
if (!iti.isValidNumber()) {
e.preventDefault();
input.setCustomValidity(
"Please, take a look to the phone number, we need a real phone number"
);
} else {
input.setCustomValidity("");
input.value = iti.getNumber();
}
});
}
input.addEventListener("input", function () {
if (iti.isValidNumber()) {
input.setCustomValidity("");
}
});
});
});