address-validation
Sample code supporting Google Maps Platform JavaScript API documentation.
author(s):
Geo Developer IX Documentation Team
HTML
<!doctype html>
<!--
@license
Copyright 2025 Google LLC. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
-->
<html>
<head>
<title>Address Validation</title>
<link rel="stylesheet" type="text/css" href="./style.css" />
<script type="module" src="./index.js"></script>
<script>
// prettier-ignore
(g=>{var h,a,k,p="The Google Maps JavaScript API",c="google",l="importLibrary",q="__ib__",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,n)=>{await (a=m.createElement("script"));e.set("libraries",[...r]+"");for(k in g)e.set(k.replace(/[A-Z]/g,t=>"_"+t[0].toLowerCase()),g[k]);e.set("callback",c+".maps."+q);a.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[q]=f;a.onerror=()=>h=n(Error(p+" could not load."));a.nonce=m.querySelector("script[nonce]")?.nonce||"";m.head.append(a)}));d[l]?console.warn(p+" only loads once. Ignoring:",g):d[l]=(f,...n)=>r.add(f)&&u().then(()=>d[l](f,...n))})({
key: "GOOGLE_MAPS_API_KEY"
});
</script>
</head>
<body>
<!-- Address Validation Form Container -->
<div id="sidebar">
<!-- Header -->
<div class="sidebar-header">
<h2>Address Validation</h2>
</div>
<!-- Content: Address Form -->
<form id="address-form" class="sidebar-content" autocomplete="off">
<!-- Example Dropdown Section -->
<div
id="example-dropdown-container"
style="
margin-bottom: 1rem;
padding-bottom: 0.75rem;
border-bottom: 1px solid #e5e7eb;
">
<label for="example-select" style="margin-bottom: 0.5rem"
>Load Example Address:</label
>
<select
...
CSS
/*
* @license
* Copyright 2025 Google LLC. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
body,
html {
height: 100%;
margin: 0;
padding: 0;
overflow: hidden;
font-family: 'Google Sans', sans-serif;
font-size: 20px;
color: #333;
}
#sidebar {
width: 800px;
max-width: calc(100% - 2rem);
background-color: #fff;
border-color: #9ca3af;
border-style: solid;
border-radius: 0.5rem;
box-shadow:
0 4px 6px -1px rgba(0, 0, 0, 0.1),
0 2px 4px -1px rgba(0, 0, 0, 0.06);
display: flex;
flex-direction: column;
overflow: hidden;
max-height: calc(100% - 2rem);
}
.sidebar-header {
padding: 0.75rem;
border-bottom: 1px solid #e5e7eb;
flex-shrink: 0;
}
.sidebar-header h2 {
margin: 0;
font-size: 1.125rem;
font-weight: 600;
color: #1f2937;
}
.sidebar-content {
padding: 0.75rem;
overflow-y: auto;
flex-grow: 1;
}
.sidebar-content::-webkit-scrollbar {
width: 6px;
}
.sidebar-content::-webkit-scrollbar-thumb {
background-color: rgba(0, 0, 0, 0.2);
border-radius: 3px;
}
#address-form > div {
margin-bottom: 0.75rem;
}
#address-form > button {
margin-top: 1rem;
}
label {
display: block;
font-size: 0.75rem;
font-weight: 500;
color: #4b5563;
margin-bottom: 0.25rem;
}
input[type='text'] {
width: 100%;
padding: 0.5rem 0.75rem;
font-size: 0.875rem;
border: 1px solid #d1d5db;
border-radius: 0.375rem;
box-sizing: border-box;
transition:
border-color 0.15s ease-in-out,
box-shadow 0.15s ease-in-out;
}
input[type='text']:focus {
outline: 0;
border-color: #2563eb;
box-shadow: 0 0 0 1px #2563eb;
}
.form-grid-triple {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 0.75rem;
}
@media (max-width: 400px) {
.form-grid-triple {
grid-template-columns: 1fr;
}
}
button {
display: inline-block;
margin-right:...
JavaScript
'use strict';
/*
* @license
* Copyright 2025 Google LLC. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
// DOM Refs
const addressForm = document.getElementById('address-form');
const clearFormButton = document.getElementById('clear-form-button');
const resultDisplay = document.getElementById('result-display');
// Input field refs
const streetAddress1Input = document.getElementById('street-address-1');
const streetAddress2Input = document.getElementById('street-address-2');
const cityInput = document.getElementById('city');
const stateInput = document.getElementById('state');
const zipCodeInput = document.getElementById('zip-code');
const regionSelect = document.getElementById('region-select');
const exampleSelect = document.getElementById('example-select');
// Core Initialization
async function init() {
// Load the Address Validation library.
await google.maps.importLibrary('addressValidation');
// Set event listeners
addressForm.addEventListener('submit', handleValidationSubmit);
exampleSelect.addEventListener('change', handleExampleSelectChange);
clearFormButton.addEventListener('click', handleClearForm);
}
// Validation Handler
async function handleValidationSubmit(event) {
event.preventDefault(); // Prevent default form submission
resultDisplay.textContent = 'Validating...'; // Clear previous results
const { AddressValidation } =
await google.maps.importLibrary('addressValidation');
// Validate the address
try {
const result = await AddressValidation.fetchAddressValidation({
address: {
regionCode: regionSelect.value.trim(),
languageCode: 'en',
addressLines: [
streetAddress1Input.value.trim(),
streetAddress2Input.value.trim(),
].filter(Boolean), // Filter out empty lines
locality: cityInput.value.trim(),
administrativeArea:...