index.html
:<!DOCTYPE html> <html lang="en"> <head> <title>International telephone input</title> <meta name="viewport" content="width=device-width, initial-scale=1" /> <link rel="stylesheet" href="styles.css" /> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.8/css/intlTelInput.css" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.8/js/intlTelInput.min.js"></script> </head> <body> <div class="container"> <form id="login" onsubmit="process(event)"> <p>Enter your phone number:</p> <input id="phone" type="tel" name="phone" /> <input type="submit" class="btn" value="Verify" /> </form> <div class="alert alert-info" style="display: none"></div> <div class="alert alert-error" style="display: none"></div> </div> </body> <script> const phoneInputField = document.querySelector("#phone"); const phoneInput = window.intlTelInput(phoneInputField, { utilsScript: "https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.8/js/utils.js", }); const info = document.querySelector(".alert-info"); const error = document.querySelector(".alert-error"); function process(event) { event.preventDefault(); const phoneNumber = phoneInput.getNumber(); info.style.display = ""; info.innerHTML = `Phone number in E.164 format: <strong>${phoneNumber}</strong>`; } </script> </html>
styles.css
in the same folder as index.html
.intl-tel-input
lookup
exports.handler = function (context, event, callback) { const response = new Twilio.Response(); response.appendHeader("Content-Type", "application/json"); response.appendHeader('Access-Control-Allow-Origin', '*'); response.appendHeader('Access-Control-Allow-Methods', 'POST, OPTIONS'); response.appendHeader('Access-Control-Allow-Headers', 'Content-Type'); if (typeof event.phone === "undefined") { response.setBody({ success: false, error: "Missing parameter; please provide a phone number.", }); response.setStatusCode(400); return callback(null, response); } const client = context.getTwilioClient(); client.lookups .phoneNumbers(event.phone) .fetch() .then((resp) => { response.setStatusCode(200); response.setBody({ success: true, }); callback(null, response); }) .catch((error) => { console.log(error); response.setStatusCode(error.status); response.setBody({ success: false, error: error.message, }); callback(null, response); }); };
{"success":true}
process
function with the following. Make sure to replace the URL inside of the fetch
call with your twilio function URL:function process(event) { event.preventDefault(); const phoneNumber = phoneInput.getNumber(); info.style.display = "none"; error.style.display = "none"; const data = new URLSearchParams(); data.append("phone", phoneNumber); fetch("http://<your-url-here>.twil.io/lookup", { method: "POST", body: data, }) .then((response) => response.json()) .then((json) => { if (json.success) { info.style.display = ""; info.innerHTML = `Phone number in E.164 format: <strong>${phoneNumber}</strong>`; } else { console.log(json.error); error.style.display = ""; error.innerHTML = `Invalid phone number.`; } }) .catch((err) => { error.style.display = ""; error.innerHTML = `Something went wrong: ${err}`; }); }
process
function code with the following:function process(event) { event.preventDefault(); const phoneNumber = phoneInput.getNumber(); info.style.display = "none"; error.style.display = "none"; if (phoneInput.isValidNumber()) { info.style.display = ""; info.innerHTML = `Phone number in E.164 format: <strong>${phoneNumber}</strong>`; } else { error.style.display = ""; error.innerHTML = `Invalid phone number.`; } }