16825 |
efrain |
1 |
<!DOCTYPE html>
|
|
|
2 |
<html>
|
|
|
3 |
<head>
|
|
|
4 |
<meta charset="utf-8">
|
|
|
5 |
<title>Example: Validation</title>
|
|
|
6 |
<link rel="stylesheet" href="../css/prism.css">
|
|
|
7 |
<link rel="stylesheet" href="../../build/css/intlTelInput.css?1603274336113">
|
|
|
8 |
<link rel="stylesheet" href="../../build/css/demo.css?1603274336113">
|
|
|
9 |
|
|
|
10 |
<link rel="stylesheet" href="../css/isValidNumber.css?1603274336113">
|
|
|
11 |
|
|
|
12 |
|
|
|
13 |
<!-- GOOGLE ANALYTICS -->
|
|
|
14 |
<script>
|
|
|
15 |
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
|
|
16 |
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
|
|
|
17 |
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
|
|
|
18 |
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
|
|
|
19 |
ga('create', 'UA-85394876-1', 'auto');
|
|
|
20 |
ga('send', 'pageview');
|
|
|
21 |
</script>
|
|
|
22 |
<!-- /GOOGLE ANALYTICS -->
|
|
|
23 |
</head>
|
|
|
24 |
|
|
|
25 |
<body>
|
|
|
26 |
<a href="/">Back</a>
|
|
|
27 |
<h1>Example: Validation</h1>
|
|
|
28 |
<p>Use the isValidNumber method (which utilises Google's libphonenumber) to validate the telephone number on the blur event.</p>
|
|
|
29 |
|
|
|
30 |
<h2>Markup</h2>
|
|
|
31 |
<pre><code class="language-markup"><input id="phone" type="tel">
|
|
|
32 |
<span id="valid-msg" class="hide">✓ Valid</span>
|
|
|
33 |
<span id="error-msg" class="hide"></span>
|
|
|
34 |
</code></pre>
|
|
|
35 |
|
|
|
36 |
<h2>Code</h2>
|
|
|
37 |
<pre><code class="language-javascript">var input = document.querySelector("#phone"),
|
|
|
38 |
errorMsg = document.querySelector("#error-msg"),
|
|
|
39 |
validMsg = document.querySelector("#valid-msg");
|
|
|
40 |
|
|
|
41 |
// here, the index maps to the error code returned from getValidationError - see readme
|
|
|
42 |
var errorMap = ["Invalid number", "Invalid country code", "Too short", "Too long", "Invalid number"];
|
|
|
43 |
|
|
|
44 |
// initialise plugin
|
|
|
45 |
var iti = window.intlTelInput(input, {
|
|
|
46 |
utilsScript: "../../build/js/utils.js?1603274336113"
|
|
|
47 |
});
|
|
|
48 |
|
|
|
49 |
var reset = function() {
|
|
|
50 |
input.classList.remove("error");
|
|
|
51 |
errorMsg.innerHTML = "";
|
|
|
52 |
errorMsg.classList.add("hide");
|
|
|
53 |
validMsg.classList.add("hide");
|
|
|
54 |
};
|
|
|
55 |
|
|
|
56 |
// on blur: validate
|
|
|
57 |
input.addEventListener('blur', function() {
|
|
|
58 |
reset();
|
|
|
59 |
if (input.value.trim()) {
|
|
|
60 |
if (iti.isValidNumber()) {
|
|
|
61 |
validMsg.classList.remove("hide");
|
|
|
62 |
} else {
|
|
|
63 |
input.classList.add("error");
|
|
|
64 |
var errorCode = iti.getValidationError();
|
|
|
65 |
errorMsg.innerHTML = errorMap[errorCode];
|
|
|
66 |
errorMsg.classList.remove("hide");
|
|
|
67 |
}
|
|
|
68 |
}
|
|
|
69 |
});
|
|
|
70 |
|
|
|
71 |
// on keyup / change flag: reset
|
|
|
72 |
input.addEventListener('change', reset);
|
|
|
73 |
input.addEventListener('keyup', reset);
|
|
|
74 |
</code></pre>
|
|
|
75 |
|
|
|
76 |
<h2>Result</h2>
|
|
|
77 |
<div id="result">
|
|
|
78 |
<input id="phone" type="tel">
|
|
|
79 |
<span id="valid-msg" class="hide">✓ Valid</span>
|
|
|
80 |
<span id="error-msg" class="hide"></span>
|
|
|
81 |
|
|
|
82 |
</div>
|
|
|
83 |
|
|
|
84 |
<script src="../js/prism.js"></script>
|
|
|
85 |
<script src="../../build/js/intlTelInput.js?1603274336113"></script>
|
|
|
86 |
<script src="./js/isValidNumber.js?1603274336113"></script>
|
|
|
87 |
</body>
|
|
|
88 |
</html>
|