How to Validate a Street Address using JavaScript

Cloudmersive
2 min readAug 5, 2022

--

Looking to include a simple data validation layer for an address input page? We’ve got you covered. Our Street Address Validation API will quickly determine if an input street address is valid or invalid, and it’ll return the latitude and longitude of the address (if valid). Here’s a quick look at a sample JSON response:

{
"ValidAddress": true,
"Latitude": 0,
"Longitude": 0
}

You can use this API for free by registering a free account on our website, and you can easily structure your API call in JavaScript with the code examples provided below.

I’ll first demonstrate how you can use JavaScript’s build-in XHR request capability to call this API. To do that, simply copy in the following code snippet:

var data = JSON.stringify({
"StreetAddress": "<string>",
"City": "<string>",
"StateOrProvince": "<string>",
"PostalCode": "<string>",
"CountryFullName": "<string>",
"CountryCode": "<string>"
});
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function() {
if(this.readyState === 4) {
console.log(this.responseText);
}
});
xhr.open("POST", "https://api.cloudmersive.com/validate/address/street-address");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Apikey", "YOUR-API-KEY-HERE");
xhr.send(data);

To instead install the jQuery library, run the below command:

bower install jquery

And then structure your API call with the following code snippet:

var settings = {
"url": "https://api.cloudmersive.com/validate/address/street-address",
"method": "POST",
"timeout": 0,
"headers": {
"Content-Type": "application/json",
"Apikey": "YOUR-API-KEY-HERE"
},
"data": JSON.stringify({
"StreetAddress": "<string>",
"City": "<string>",
"StateOrProvince": "<string>",
"PostalCode": "<string>",
"CountryFullName": "<string>",
"CountryCode": "<string>"
}),
};
$.ajax(settings).done(function (response) {
console.log(response);
});

That’s all you need — now you can validate street address inputs with ease.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.