Last active
December 19, 2015 06:59
-
-
Save i2bskn/5915686 to your computer and use it in GitHub Desktop.
zipcloudで郵便番号から住所の一部自動入力
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(){ | |
const ZIP = "#user_zip"; | |
const ADDRESS = "#user_address"; | |
var insert_addr = function(zip_code){ | |
$.ajax({ | |
url: "http://zipcloud.ibsnet.co.jp/api/search", | |
data: { zipcode: zip_code }, | |
dataType: "jsonp", | |
success: function(data){ | |
if (data.status === 200 && data.results != null){ | |
var addr = [data.results[0].address1, data.results[0].address2, data.results[0].address3]; | |
$(ADDRESS).val(addr.join("")); | |
}; | |
} | |
}); | |
}; | |
$(function(){ | |
$(ZIP).on("keyup", function(){ | |
var zip_code = $(this).val(); | |
if (zip_code.match(/[^0-9]/)) { | |
zip_code = zip_code.replace(/[^0-9]/g, ""); | |
$(this).val(zip_code); | |
}; | |
if (zip_code.length === 7) { | |
insert_addr($(this).val()); | |
}; | |
}); | |
}); | |
}).call(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment