入力された文字列のバイト数を計算し、指定された制限を超えた場合にアラートを表示
<input type="text" id="textBox" placeholder="文字を入力してください"> <script> function checkByteLength(inputElement, maxBytes) { const text = inputElement.value; let byteLength = new TextEncoder().encode(text).length; // バイト数を計算する if (byteLength > maxBytes) { const overBytes = byteLength - maxBytes; alert(`${overBytes}バイトオーバーです`); } } // 例: テキストボックスの入力イベントにバイト数チェックを追加 const textBox = document.getElementById("textBox"); textBox.addEventListener("input", () => checkByteLength(textBox, 50)); // 50バイトが制限 </script>