Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <html>
<head>
    <title>ボタンクリックイベント</title>
</head>
<body>
<h1>郵便番号検索</h1>
<p>郵便番号を入力してください</p>
<input type="text" id="postcode">
<input type="button" value="確認" onclick="checkPostCode()">
<p id="sysMsg"></p>
</body>
</html>
              
            
!

CSS

              
                
              
            
!

JS

              
                // 入力文字が郵便番号か確認する関数
function checkPostCode() {
    // メッセージ初期化
    var sysMsgElement = document.getElementById("sysMsg");
    sysMsgElement.innerHTML = "";
    sysMsgElement.style.color = "red";
    // 入力値取得
    var inputValue = document.getElementById("postcode").value;
    // 未入力判定
    if (inputValue == "") {
        sysMsgElement.innerHTML = "未入力です";
        return;
    }
    // 正規表現パターン: 全角半角数値判定
    var numericPattern = /^[0-90-9]+$/;
    if (!numericPattern.test(inputValue)) {
        sysMsgElement.innerHTML = "数値のみ入力してください";
        return;
    }
    // 正規表現パターン: 文字数(7文字)判定
    var countPattern = /^.{7}$/;
    if (!countPattern.test(inputValue)) {
        sysMsgElement.innerHTML = "7文字で入力してください";
        return;
    }
    // 正規表現パターン: 先頭文字(1以上)判定
    var headPattern = /^[1-9]|[1-9]*$/;
    if (!headPattern.test(inputValue)) {
        sysMsgElement.innerHTML = "先頭文字は1以上で入力してください";
        return;
    }
    var apiUrl = "https://zipcloud.ibsnet.co.jp/api/search?zipcode=" + inputValue;
    // GETリクエストを送信
    fetch(apiUrl)
        .then(response => {
            // レスポンスが正常に受信された場合
            if (response.ok) {
                return response.json(); // JSONデータを取得
            } else {
                throw new Error('APIリクエストに失敗しました。');
            }
        })
        .then(data => {
            // レスポンスデータを処理
            console.log(data);
            if (data.results === null){
                sysMsgElement.innerHTML = "該当する郵便番号が見つかりません。";
            } else {
                var outputValue  = "郵便番号: " + data.results[0].zipcode;
                    outputValue += "<br>都道府県コード: " + data.results[0].prefcode;
                    outputValue += "<br>住所: " + data.results[0].address1 + "&nbsp;" + data.results[0].address2 + "&nbsp;" + data.results[0].address3;
                    outputValue += "<br>住所(カナ): " + data.results[0].kana1 + "&nbsp;" + data.results[0].kana2 + "&nbsp;" + data.results[0].kana3;
                sysMsgElement.innerHTML = outputValue;
                sysMsgElement.style.color = "blue";
            }
        })
        .catch(error => {
            // エラーハンドリング
            console.error(error);
        });
}
              
            
!
999px

Console