본문 바로가기
Front-end/jQuery

[jQuery] Textarea 용량(Byte) 표시 / 최대용량제한

by Gi-One 2024. 8. 23.

#textarea #Byte #최대용량제한 #jQuery

 

 

 

Textarea에 최대 글자수는 자주 적용 해봤지만 스크립트로 Byte도 제한이 가능합니다.

혹시 모르니 최대글자수도 2000자로 적용 해놓고 작업 했습니다.

 

2000자 넘게 글을 입력하려고 하면 #content_byte에 full 클래스가 추가되며 빨간색으로 표시됩니다.

 

 

HTML

  <form action="">
    <div class="frm-ipt-box frm-content-ipt-box">
      <div class="frm-ipt-line">
        <h3 class="ipt-tit frm-required">
          내용
        </h3>
        <div class="ipt-smallbox">
          <textarea name="wr_content" id="wr_content" class="n-ipt" maxlength="2000" required></textarea>
        </div>
      </div>
      <div class="bottom-byte">
        <p>
          <span class="now" id="content_byte">0</span>
          <span>/ 2,000Byte</span>
        </p>
      </div>
    </div>

    <div class="submit-box">
      <button type='submit' class='frm-submitbtn'>등록</button>
    </div>
  </form>

 

CSS


body{
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  min-height: 600px;
}
form{
  max-width: 1200px;
  width: 100%;
  margin: 0 auto;
  padding: 50px 30px;
  border: 1px solid #f1f1f1;
}
.frm-ipt-box .frm-ipt-line{
  display: flex;
  align-items: center;
  gap: 12px;
}
.frm-ipt-box .ipt-tit{
  width: 120px;
}
.frm-ipt-box .ipt-tit > span{
  font-size: 20px;
  font-weight: normal;
  letter-spacing: -0.8px;
  color: rgb(0, 0, 0);
  position: relative;
}
.frm-required::after{
  content: "";
  display: block;
  width: 6px;
  height: 6px;
  background-color: rgb(230, 0, 18);
  border-radius: 100%;
  position: absolute;
  top: 0;
  right: -5px;
}
.frm-ipt-box .ipt-smallbox{
  width: calc(100% - 132px);
}
.n-ipt{
  width: 100%;
  background-color: rgb(248, 248, 248);
  outline: none;
  border: none;
  padding: 23px 37px;
  font-size: 20px;
  font-weight: normal;
  letter-spacing: -0.8px;
  text-align: left;
  color: rgb(0, 0, 0);
}

.frm-ipt-box.frm-content-ipt-box .bottom-byte{
  margin-top: 7px;
}
.frm-ipt-box.frm-content-ipt-box .bottom-byte p{
  font-size: 15px;
  font-weight: 300;
  letter-spacing: -0.6px;
  color: rgb(143, 143, 143);
  text-align: right;
}
.frm-ipt-box.frm-content-ipt-box .bottom-byte p .full{
  color: red;
  font-weight: bold;
}
.frm-ipt-box.frm-content-ipt-box .bottom-byte,
.frm-ipt-box.frm-email-ipt-box .bottom-txt,
.frm-ipt-box .file-bottom-txt{
  padding-left: 132px;
}

.frm-ipt-box.frm-content-ipt-box textarea.n-ipt{
  height: 220px;
  overflow-y: auto;
}

.frm-ipt-box.frm-content-ipt-box textarea.n-ipt::-webkit-scrollbar {
  width: 4px;
  background-color: rgb(209, 209, 209);
}
.frm-ipt-box.frm-content-ipt-box textarea.n-ipt::-webkit-scrollbar-thumb {
  background-color: rgb(125, 125, 125);
}
textarea.n-ipt{
  resize: none;
  -webkit-resize : none;
}
.submit-box button{
  display: block;
  width: 300px;
  font-size: 24px;
  font-weight: bold;
  border: 1px solid #ccc;
  text-align: center;
  border-radius: 5px;
  padding: 12px 0;
  margin: 0 auto;
  margin-top: 50px;
}

 

 

jQuery

 <script>
  $(document).ready(function() {
    function getByteLength(val) {
    let byteLength = 0;
    for (let i = 0; i < val.length; i++) {
      const charCode = val.charCodeAt(i);
      if (charCode <= 0x007f) {
        byteLength += 1;
      } else if (charCode <= 0x07ff) {
        byteLength += 2;
      } else if (charCode <= 0xffff) {
        byteLength += 3;
      } else {
        byteLength += 4;
      }
    }
    return byteLength;
  }

  function updateByteCount() {
    let content = $('#wr_content').val();
    let byteCount = getByteLength(content);
    
    if (byteCount >= 2000) {
      $('#content_byte').addClass('full');
      
      // Truncate content to fit within 2000 bytes
      let truncatedContent = '';
      let truncatedByteCount = 0;
      for (let i = 0; i < content.length; i++) {
        const charCode = content.charCodeAt(i);
        let charByteLength = 0;
        if (charCode <= 0x007f) {
          charByteLength = 1;
        } else if (charCode <= 0x07ff) {
          charByteLength = 2;
        } else if (charCode <= 0xffff) {
          charByteLength = 3;
        } else {
          charByteLength = 4;
        }

        if (truncatedByteCount + charByteLength > 2000) {
          break;
        }

        truncatedContent += content[i];
        truncatedByteCount += charByteLength;
      }

      $('#wr_content').val(truncatedContent);
      byteCount = truncatedByteCount;
    } else {
      $('#content_byte').removeClass('full');
    }

    $('#content_byte').text(byteCount + ' Bytes');
  }

    $('#wr_content').on('input', updateByteCount);


    // wr_content byte
    });
  </script>

 

 

댓글