-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
demo.html
97 lines (90 loc) · 3.54 KB
/
demo.html
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>International Telephone Input</title>
<link rel="stylesheet" href="build/css/intlTelInput.css" />
<link rel="stylesheet" href="build/css/demo.css" />
</head>
<body>
<h1>International Telephone Input</h1>
<form>
<input id="phone" name="phone" type="tel" value="" />
<button class="button" id="btn" type="button">Validate</button>
<span id="valid-msg" class="hide"></span>
<span id="error-msg" class="hide"></span>
</form>
<script src="build/js/intlTelInputWithUtils.js"></script>
<script>
const input = document.querySelector("#phone");
const iti = window.intlTelInput(input, {
// allowDropdown: false,
// autoPlaceholder: "off",
// containerClass: "test",
// countryOrder: ["jp", "kr"],
// countrySearch: false,
// customPlaceholder: function(selectedCountryPlaceholder, selectedCountryData) {
// return "e.g. " + selectedCountryPlaceholder;
// },
// dropdownContainer: document.querySelector('#custom-container'),
// excludeCountries: ["us"],
// fixDropdownWidth: false,
// formatAsYouType: false,
// formatOnDisplay: false,
// geoIpLookup: function(callback) {
// fetch("https://ipapi.co/json")
// .then(function(res) { return res.json(); })
// .then(function(data) { callback(data.country_code); })
// .catch(function() { callback(); });
// },
// hiddenInput: () => ({ phone: "phone_full", country: "country_code" }),
// i18n: { 'de': 'Deutschland' },
initialCountry: "us",
// loadUtils: () => import("/build/js/utils.js"), // leading slash (and http-server) required for this to work in chrome
// nationalMode: false,
// onlyCountries: ['us', 'gb', 'ch', 'ca', 'do'],
// placeholderNumberType: "MOBILE",
// showFlags: false,
// separateDialCode: true,
// strictMode: true,
// useFullscreenPopup: true,
// validationNumberTypes: null,
});
window.iti = iti; // useful for testing
const button = document.querySelector("#btn");
const errorMsg = document.querySelector("#error-msg");
const validMsg = document.querySelector("#valid-msg");
const errorMap = ["Invalid number", "Invalid country code", "Too short", "Too long", "Invalid number"];
const reset = () => {
input.classList.remove("error");
errorMsg.innerHTML = "";
validMsg.innerHTML = "";
errorMsg.classList.add("hide");
validMsg.classList.add("hide");
};
const showError = (msg) => {
input.classList.add("error");
errorMsg.innerHTML = msg;
errorMsg.classList.remove("hide");
};
// on click button: validate
button.addEventListener('click', () => {
reset();
if (!input.value.trim()) {
showError("Required");
} else if (iti.isValidNumber()) {
validMsg.innerHTML = "Valid number: " + iti.getNumber();
validMsg.classList.remove("hide");
} else {
const errorCode = iti.getValidationError();
const msg = errorMap[errorCode] || "Invalid number";
showError(msg);
}
});
// on keyup / change flag: reset
input.addEventListener('change', reset);
input.addEventListener('keyup', reset);
</script>
</body>
</html>