본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

문자 수 세기

원문 보기 위키 갱신

문자 수 세기 (Character count)

문자 수에 제한이 있을 때 사용자가 얼마나 많은 텍스트를 입력할 수 있는지 알 수 있도록 도와주는 컴포넌트예요.

출처: 문서

본문

어떤 때 사용하나요

항상 먼저 문자 수 세기 없이 서비스를 테스트해 보세요.

사용자가 입력할 수 있는 문자 수를 제한할 타당한 이유가 있을 때만 문자 수 세기 컴포넌트를 사용하세요. 예를 들어 다음과 같은 경우가 그렇죠:

  • 사용자가 필요한 것보다 더 많은 정보를 입력할 가능성이 있다는 증거가 있을 때
  • 입력이 특정 문자 수를 넘을 수 없는 법적·기술적 이유가 있을 때

어떤 때 사용하지 말아야 하나요

사용자가 계속해서 서비스 백엔드가 부과한 문자 수 제한에 부딪힌다면, 문자 수 세기를 사용하기보다 그 제한을 늘리는 것을 시도해 보세요.

어떻게 동작하나요

문자 수 제한이 있는 Textarea 컴포넌트에 입력하면서 남은 문자 수를 사용자에게 알려줘요.

사용자는 자신이 textarea와 상호작용하는 방식에 가장 잘 맞는 속도로 업데이트를 받아요. 즉:

  • 시력이 있는 사용자는 입력하면서 업데이트되는 개수 메시지를 볼 수 있어요
  • 스크린 리더 사용자는 입력을 멈추면 개수 안내를 들을 수 있어요

이 컴포넌트는 사용자가 정보를 입력하는 것을 막지 않아요. 사용자는 문자 수 제한보다 더 많이 입력할 수 있지만, 너무 많이 입력했다는 안내를 받아요. 이를 통해 사용자가 전체 답변을 입력하거나 붙여넣은 다음에 줄여서 수정할 수 있어요.

개수 메시지는 textarea 아래에 표시되는데, 그 이유는:

  • textarea 위의 힌트 텍스트나 오류 메시지와 명확하게 분리되도록
  • 표시 영역 아래에 있다면, 사용자가 응답을 보내려고 스크롤을 내릴 때 다시 볼 수 있도록

이 컴포넌트는 JavaScript를 사용해요. JavaScript를 사용할 수 없다면, 사용자는 개수 메시지 대신 "지정된 문자 수를 입력할 수 있다"는 내용의 정적 메시지를 보게 돼요.

문자 수 세기 컴포넌트를 쓰는 방법은 두 가지예요. HTML을 쓸 수도 있고, Nunjucks나 GOV.UK Prototype Kit을 쓰고 있다면 Nunjucks 매크로를 쓸 수도 있어요.

<div class="govuk-form-group govuk-character-count" data-module="govuk-character-count" data-maxlength="200">
  <h1 class="govuk-label-wrapper">
    <label class="govuk-label govuk-label--l" for="with-hint">
      Can you provide more detail?
    </label>
  </h1>
  <div id="with-hint-hint" class="govuk-hint">
    Do not include personal or financial information like your National Insurance number or credit card details
  </div>
  <textarea class="govuk-textarea govuk-js-character-count" id="with-hint" name="withHint" rows="5" aria-describedby="with-hint-info with-hint-hint"></textarea>
  <div id="with-hint-info" class="govuk-hint govuk-character-count__message">
    You can enter up to 200 characters
  </div>
</div>
{% from "govuk/components/character-count/macro.njk" import govukCharacterCount %}

{{ govukCharacterCount({
  name: "withHint",
  id: "with-hint",
  maxlength: 200,
  label: {
    text: "Can you provide more detail?",
    classes: "govuk-label--l",
    isPageHeading: true
  },
  hint: {
    text: "Do not include personal or financial information like your National Insurance number or credit card details"
  }
}) }}

페이지에서 한 개 이상의 질문을 묻는 경우

페이지에서 한 개 이상의 질문을 묻는다면, <label>의 내용을 페이지 제목으로 설정하지 마세요. Question pages에서 여러 질문을 묻는 방법에 대해 자세히 알아보세요.

<div class="govuk-form-group govuk-character-count" data-module="govuk-character-count" data-maxlength="200">
  <label class="govuk-label" for="label-as-page-heading">
    Describe the nature of your event
  </label>
  <textarea class="govuk-textarea govuk-js-character-count" id="label-as-page-heading" name="labelAsPageHeading" rows="5" aria-describedby="label-as-page-heading-info"></textarea>
  <div id="label-as-page-heading-info" class="govuk-hint govuk-character-count__message">
    You can enter up to 200 characters
  </div>
</div>
{% from "govuk/components/character-count/macro.njk" import govukCharacterCount %}

{{ govukCharacterCount({
  id: "label-as-page-heading",
  name: "labelAsPageHeading",
  maxlength: 200,
  label: {
    text: "Describe the nature of your event"
  }
}) }}

단어 수가 더 유용한지 고려하기

어떤 경우에는 단어 수(word count)를 보여주는 것이 더 유용할 수 있어요. 예를 들어 질문에 더 긴 답변이 필요한 경우가 그렇죠.

컴포넌트 마크업에서 data-maxwords를 설정하면 돼요. 예를 들어 data-maxwords="150"은 150이라는 단어 제한을 설정해요.

<div class="govuk-form-group govuk-character-count" data-module="govuk-character-count" data-maxwords="150">
  <h1 class="govuk-label-wrapper">
    <label class="govuk-label govuk-label--l" for="word-count">
      Enter a job description
    </label>
  </h1>
  <textarea class="govuk-textarea govuk-js-character-count" id="word-count" name="wordCount" rows="5" aria-describedby="word-count-info"></textarea>
  <div id="word-count-info" class="govuk-hint govuk-character-count__message">
    You can enter up to 150 words
  </div>
</div>
{% from "govuk/components/character-count/macro.njk" import govukCharacterCount %}

{{ govukCharacterCount({
  name: "wordCount",
  id: "word-count",
  maxwords: 150,
  label: {
    text: "Enter a job description",
    classes: "govuk-label--l",
    isPageHeading: true
  }
}) }}

좁은 제한 피하기

문자 수 세기를 사용할 때는 대부분의 사용자가 필요로 하는 것보다 제한을 높게 설정하려고 하세요. 사용자 연구와 데이터 분석을 통해 이것이 무엇인지 알아내세요.

제한이 사용자가 도달할 가능성이 있는 것보다 훨씬 높은 경우

textarea 안의 텍스트 길이가 특정 '임계값(threshold)' 문자를 넘어설 때만 문자 수 메시지를 표시하도록 선택할 수 있어요. 이는 서비스의 기술적 한계 때문에 문자 제한이 필요하지만, 사용자가 그 제한에 도달할 가능성이 낮을 때 유용해요.

이렇게 하려면 컴포넌트 마크업에서 임계값을 백분율로 설정하세요. 예를 들어 data-threshold="75"은 사용자가 제한의 75% 이상에 해당하는 텍스트 길이를 입력했을 때만 개수 메시지를 보여줘요.

임계값 옵션을 사용하는 textarea와 처음 상호작용할 때 스크린 리더 사용자는 문자 제한을 듣게 돼요. 시력이 있는 사용자는 개수 메시지가 표시될 때까지 아무것도 보지 못해요 – 물론 힌트 텍스트에 문자 제한을 포함하기로 선택할 수도 있어요.

<div class="govuk-form-group govuk-character-count" data-module="govuk-character-count" data-maxlength="1000" data-threshold="85">
  <h1 class="govuk-label-wrapper">
    <label class="govuk-label govuk-label--l" for="threshold">
      Can you provide more detail?
    </label>
  </h1>
  <textarea class="govuk-textarea govuk-js-character-count" id="threshold" name="threshold" rows="5" aria-describedby="threshold-info">This example of a textarea has a character limit of 1000 characters. The character count is hidden, but will appear when more than 850 characters are entered in this textarea. Type some more text into this textarea to see the character count appear. This paragraph will now repeat 2 more times.

This example of a textarea has a character limit of 1000 characters. The character count is hidden, but will appear when more than 850 characters are entered in this textarea. Type some more text into this textarea to see the character count appear. This paragraph will now repeat 1 more time.

This example of a textarea has a character limit of 1000 characters. The character count is hidden, but will appear when more than 850 characters are entered in this textarea. Type some more text into this textarea to see the character count appear.</textarea>
  <div id="threshold-info" class="govuk-hint govuk-character-count__message">
    You can enter up to 1000 characters
  </div>
</div>
{% from "govuk/components/character-count/macro.njk" import govukCharacterCount %}

{% set textareaValue -%}
This example of a textarea has a character limit of 1000 characters. The character count is hidden, but will appear when more than 850 characters are entered in this textarea. Type some more text into this textarea to see the character count appear. This paragraph will now repeat 2 more times.

This example of a textarea has a character limit of 1000 characters. The character count is hidden, but will appear when more than 850 characters are entered in this textarea. Type some more text into this textarea to see the character count appear. This paragraph will now repeat 1 more time.

This example of a textarea has a character limit of 1000 characters. The character count is hidden, but will appear when more than 850 characters are entered in this textarea. Type some more text into this textarea to see the character count appear.
{%- endset %}

{{ govukCharacterCount({
  name: "threshold",
  id: "threshold",
  maxlength: 1000,
  threshold: 85,
  value: textareaValue,
  label: {
    text: "Can you provide more detail?",
    classes: "govuk-label--l",
    isPageHeading: true
  }
}) }}

오류 메시지

오류 메시지는 다음과 같이 스타일을 지정해야 해요:

<div class="govuk-form-group govuk-form-group--error govuk-character-count" data-module="govuk-character-count" data-maxlength="350">
  <h1 class="govuk-label-wrapper">
    <label class="govuk-label govuk-label--l" for="exceeding-characters">
      Enter a job description
    </label>
  </h1>
  <p id="exceeding-characters-error" class="govuk-error-message">
    <span class="govuk-visually-hidden">Error:</span> Job description must be 350 characters or less
  </p>
  <textarea class="govuk-textarea govuk-textarea--error govuk-js-character-count" id="exceeding-characters" name="exceeding" rows="5" aria-describedby="exceeding-characters-info exceeding-characters-error">A content designer works on the end-to-end journey of a service to help users complete their goal and government deliver a policy intent. Their work may involve the creation of, or change to, a transaction, product or single piece of content that stretches across digital and offline channels. They make sure appropriate content is shown to a user in the right place and in the best format.</textarea>
  <div id="exceeding-characters-info" class="govuk-hint govuk-character-count__message">
    You can enter up to 350 characters
  </div>
</div>
{% from "govuk/components/character-count/macro.njk" import govukCharacterCount %}

{{ govukCharacterCount({
  id: "exceeding-characters",
  name: "exceeding",
  maxlength: 350,
  value: "A content designer works on the end-to-end journey of a service to help users complete their goal and government deliver a policy intent. Their work may involve the creation of, or change to, a transaction, product or single piece of content that stretches across digital and offline channels. They make sure appropriate content is shown to a user in the right place and in the best format.",
  label: {
    text: "Enter a job description",
    classes: "govuk-label--l",
    isPageHeading: true
  },
  errorMessage: {
    text: "Job description must be 350 characters or less"
  }
}) }}

사용자가 문자 수가 너무 많은 응답을 보내려고 하면, 필드 위에 오류 메시지를 보여줘야 하고 그 아래에도 개수 메시지를 보여줘야 해요.

오류 메시지는 무엇이 잘못되었는지, 어떻게 고치는지 사용자에게 알려줘요. 개수 메시지는 사용자가 입력하면서 실시간으로 업데이트되는 피드백을 제공해요.

입력란은 사용자가 문자 수 제한을 초과하려고 할 때만 빨간 테두리를 보여줘요. 문자 수가 제한 안에 있으면 오류가 있어도 이 테두리를 보여주지 않아요. 사용자가 입력을 시작하면 테두리가 사라지는 것이 사용자에게 어려움을 줄 수 있다고 판단했거든요.

오류가 오류 메시지 작성에 대한 GOV.UK 안내를 따르고, 특정 오류 상태에 대해 구체적인 오류 메시지를 갖도록 하세요.

입력란이 비어 있는 경우

'Enter [whatever it is]'라고 말하세요. 예를 들어 'Enter a summary'처럼요.

입력이 너무 긴 경우

'[whatever it is] must be [number] characters or less'라고 말하세요. 예를 들어 'Summary must be 400 characters or less'처럼요.

이 컴포넌트에 대한 연구

2017년에 Government Digital Service(GDS)는 'Apply for a temporary event notice' 서비스의 프로토타입에서 이 컴포넌트를 개발하고 테스트했어요. 이 연구 동안 컴포넌트는 디지털 기술이 낮은 사용자와 장애가 있는 사용자를 포함한 17명의 사용자로 테스트되었어요.

2022년에 GOV.UK Design System 팀은 일부 스크린 리더가 문자 수를 두 번 안내하지 않도록 컴포넌트를 더 접근성 있게 업데이트했어요.

알려진 문제와 한계

Internet Explorer 11에서 JAWS는 설정된 임계값을 무시하고, 사용자가 임계값보다 적게 입력했더라도 문자 수를 안내해요.

Chrome 버전 99에서 JAWS는 미리 채워진 textarea의 힌트나 문자 수를 안내하지 않아요. 이것은 JAWS 개발자에게 알려진 문제예요.

또한 이 컴포넌트는 일부 문자를 여러 문자로 계산해요. 예를 들어 이모지와 일부 비라틴 문자처럼요.

이 컴포넌트를 사용하는 서비스

이 컴포넌트는 다음을 포함한 여러 서비스에서 사용돼요.

  • Department for Education – 교사 연수 과정 게시(Publish teacher training courses)
  • Government Digital Service – 콘텐츠 게시자 애플리케이션(Content publisher application)

다음 단계

다음을 알아내기 위해 더 많은 사용자 연구가 필요해요:

  • 문자 제한과 단어 제한 사이에서 어떻게 결정할지
  • 제한을 초과하는 문자를 빨간색으로 강조하는 것이 사용자에게 도움이 되는지
  • 컴포넌트가 상한뿐 아니라 하한과도 어떻게 동작할지
  • 텍스트 입력(text inputs)에서 문자 수 세기를 활성화하는 것이 유용할지

이 컴포넌트를 사용해 보셨다면, 사용자 연구 결과를 공유하기 위해 연락해 주세요.

최근 변경 사항

날짜 변경 설명
2025년 5월 GOV.UK Frontend v5.10.1 오류 메시지가 있을 때 렌더링되는 오류 테두리 색상을 추가했어요.
2025년 3월 GOV.UK Frontend v5.9.0 컴포넌트 id 속성이 기본적으로 name 파라미터를 기반으로 하도록 변경했어요. 포커스된 오류 상태를 추가했어요.
2024년 10월 안내 v4.1.0에서 개선한 접근성 개선 사항에 대한 세부 내용을 추가했어요.
2024년 3월 GOV.UK Frontend v5.3.0 컴포넌트 래퍼와 폼 그룹을 병합하도록 HTML 구조를 변경했어요.
2023년 12월 GOV.UK Frontend v5.0.0 요소가 없을 때 인스턴스화 중 ElementError를 던지도록 했어요.

전체 변경 이력을 보려면 위 출처 문서를 확인하세요.

이 컴포넌트 개선에 도움 주기

이 페이지가 유용하고, 관련성이 있고, 최신 상태로 유지되도록 하려면:

  • GitHub의 'Character count' 토론에 참여해서 여러분의 연구를 공유하세요
  • GitHub에서 변경을 제안하세요 – GitHub에서 변경을 제안하는 방법에 대해 자세히 알아보세요

더 알아보기 (Learn more)