Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to write script for Address validation of a field with usps api integration

Community Alums
Not applicable

How to write script for Address validation of a field with usps api integration

1 REPLY 1

Ruhee Gupta
Tera Contributor

function validateAddress() {
var address = g_form.getValue('address_field'); // Get the value of the address field
// Make a call to the address validation service API
// Replace 'YOUR_API_KEY' and 'YOUR_ADDRESS_VALIDATION_ENDPOINT' with your actual API key and endpoint
var url = 'YOUR_ADDRESS_VALIDATION_ENDPOINT?address=' + encodeURIComponent(address) + '&key=YOUR_API_KEY';
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
var response = JSON.parse(xhr.responseText);
// Process the response to determine if the address is valid
if (response.valid) {
alert('Address is valid!');
} else {
alert('Invalid address. Please check and try again.');
}
} else {
console.error('Error: Unable to validate address.');
}
}
};
xhr.send();
}

 

Please mark my answer helpful if it helped you.