Created
July 21, 2019 20:05
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<input type="text" pattern="\d*" class="form-control" id="postal" placeholder="0000" maxlength="4" required onkeyup="autofillCity(this.value)"> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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