Skip to content

Instantly share code, notes, and snippets.

@matsubo
Created May 13, 2021
Embed
What would you like to do?
$(function() {
// handling bank code
$('#postcode').keyup(function(e){
if (!(48 <= e.keyCode && e.keyCode <= 57)) { // 0 - 9
return;
}
$('#prefecture').val('');
$('#city').val('');
$('#suburb').val('');
postcode = $('#postcode').val();
if (postcode.length != 7) {
return;
}
$.getJSON("https://postcode.teraren.com/postcodes/" + postcode + ".json", function(json){
$('#prefecture').val(json.prefecture);
$('#city').val(json.city);
$('#suburb').val(json.suburb);
}).fail(function(jqXHR, textStatus, errorThrown) {
$('#prefecture').val('');
$('#city').val('');
$('#suburb').val('');
});
});
<form>
<input type="text" id="postcode" placeholder="1600023" maxlength="7" autofocus>
<input type="text" id="prefecture" placeholder="東京都" disabled>
<input type="text" id="city" placeholder="新宿区" disabled>
<input type="text" id="suburb" placeholder="西新宿" disabled>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment