89 lines
3.1 KiB
HTML
89 lines
3.1 KiB
HTML
|
<!DOCTYPE html>
|
||
|
<html>
|
||
|
<head>
|
||
|
<meta charset="utf-8">
|
||
|
<title>Example: Validation</title>
|
||
|
<link rel="stylesheet" href="../css/prism.css">
|
||
|
<link rel="stylesheet" href="../../build/css/intlTelInput.css?1541153396801">
|
||
|
<link rel="stylesheet" href="../../build/css/demo.css?1541153396801">
|
||
|
|
||
|
<link rel="stylesheet" href="../css/isValidNumber.css?1541153396801">
|
||
|
|
||
|
|
||
|
<!-- GOOGLE ANALYTICS -->
|
||
|
<script>
|
||
|
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
||
|
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
|
||
|
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
|
||
|
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
|
||
|
ga('create', 'UA-85394876-1', 'auto');
|
||
|
ga('send', 'pageview');
|
||
|
</script>
|
||
|
<!-- /GOOGLE ANALYTICS -->
|
||
|
</head>
|
||
|
|
||
|
<body>
|
||
|
<a href="/">Back</a>
|
||
|
<h1>Example: Validation</h1>
|
||
|
<p>Use the isValidNumber method (which utilises Google's libphonenumber) to validate the telephone number on the blur event.</p>
|
||
|
|
||
|
<h2>Markup</h2>
|
||
|
<pre><code class="language-markup"><input id="phone" type="tel">
|
||
|
<span id="valid-msg" class="hide">✓ Valid</span>
|
||
|
<span id="error-msg" class="hide"></span>
|
||
|
</code></pre>
|
||
|
|
||
|
<h2>Code</h2>
|
||
|
<pre><code class="language-javascript">var input = document.querySelector("#phone"),
|
||
|
errorMsg = document.querySelector("#error-msg"),
|
||
|
validMsg = document.querySelector("#valid-msg");
|
||
|
|
||
|
// here, the index maps to the error code returned from getValidationError - see readme
|
||
|
var errorMap = [ "Invalid number", "Invalid country code", "Too short", "Too long", "Invalid number"];
|
||
|
|
||
|
// initialise plugin
|
||
|
var iti = window.intlTelInput(input, {
|
||
|
utilsScript: "../../build/js/utils.js?1541153396801"
|
||
|
});
|
||
|
|
||
|
var reset = function() {
|
||
|
input.classList.remove("error");
|
||
|
errorMsg.innerHTML = "";
|
||
|
errorMsg.classList.add("hide");
|
||
|
validMsg.classList.add("hide");
|
||
|
};
|
||
|
|
||
|
// on blur: validate
|
||
|
input.addEventListener('blur', function() {
|
||
|
reset();
|
||
|
if (input.value.trim()) {
|
||
|
if (iti.isValidNumber()) {
|
||
|
validMsg.classList.remove("hide");
|
||
|
} else {
|
||
|
input.classList.add("error");
|
||
|
var errorCode = iti.getValidationError();
|
||
|
errorMsg.innerHTML = errorMap[errorCode];
|
||
|
errorMsg.classList.remove("hide");
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
|
||
|
// on keyup / change flag: reset
|
||
|
input.addEventListener('change', reset);
|
||
|
input.addEventListener('keyup', reset);
|
||
|
</code></pre>
|
||
|
|
||
|
<h2>Result</h2>
|
||
|
<div id="result">
|
||
|
<input id="phone" type="tel">
|
||
|
<span id="valid-msg" class="hide">✓ Valid</span>
|
||
|
<span id="error-msg" class="hide"></span>
|
||
|
|
||
|
</div>
|
||
|
|
||
|
<script src="../js/prism.js"></script>
|
||
|
<script src="../../build/js/intlTelInput.js?1541153396801"></script>
|
||
|
<script src="./js/isValidNumber.js?1541153396801"></script>
|
||
|
</body>
|
||
|
</html>
|