Created
May 8, 2018 13:47
-
-
Save sakahukamaki/3c12ba97e86161a6d5044cb953a77cc9 to your computer and use it in GitHub Desktop.
郵便番号を入力されたらもろもろ整形してハイフン含む形に成型する
This file contains hidden or 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
// 全角数字を半角数字に変更 | |
// -> 半角数字以外を除去 | |
// -> xxx-xxxxの形に変更 | |
function fixPostalCode(postal_code) { | |
const fixed = postal_code | |
.replace(/[0-9]/g, function(s) { | |
return String.fromCharCode(s.charCodeAt(0) - 65248); | |
}) | |
.replace(/[^0-9]/g, '') | |
.replace(/^([0-9]{3})([0-9]{0,4}).*/, "$1-$2"); | |
return fixed; | |
} | |
$(document).on('turbolinks:load', function() { | |
$("#input_postal_code").on("blur", function(){ | |
let postal_code = $(this).val(); | |
postal_code = fixPostalCode(postal_code); | |
$(this).val(postal_code); | |
}); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment