Skip to content

Instantly share code, notes, and snippets.

@OscarKolsrud
Created July 21, 2019 20:05
Show Gist options
  • Save OscarKolsrud/e295e1c318d923ee88e1d365de0ecd3b to your computer and use it in GitHub Desktop.
Save OscarKolsrud/e295e1c318d923ee88e1d365de0ecd3b to your computer and use it in GitHub Desktop.
Postnummer autofill - Henter postnummer fra bring og autofiller en form input med det
<input type="text" pattern="\d*" class="form-control" id="postal" placeholder="0000" maxlength="4" required onkeyup="autofillCity(this.value)">
function autofillCity(postnummer) {
if (postnummer.length == 4) {
$.ajax({
url: 'https://api.bring.com/shippingguide/api/postalCode.json?clientUrl=https://alfawiz.kolsrudweb.no/kunde/ny&pnr=' + postnummer + '',
method: 'get',
dataType: 'json',
crossDomain: true,
success: function(data) {
if (data.valid == true) {
document.getElementById("city").value = data.result;
return true;
} else {
document.getElementById("city").value = "Ugyldig postnummer";
return false;
}
}
});
} else {
document.getElementById("city").value = "Skriv inn postnummer...";
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment