본문으로 건너뛰기Skip to content
예제 첫화면Examples home

글자 입력 규칙: 대소문자, 가리기, 형식 검사Text input rules: case, secret and pattern

editorParams의 case·secret·pattern·patternMessage로 글자 입력을 정해요. 상품 코드는 대문자로 바뀌고 형식(영문 2자와 숫자 3자)을 입력하는 동안 안내하며, 비밀번호 칸은 가려져요.editorParams case, secret, pattern and patternMessage shape text input. A product code turns upper-case and is checked against a format (2 letters and 3 digits) while typing, and a password cell is hidden.

활용Intermediate
직접 써 보기Live preview글자 입력 규칙: 대소문자, 가리기, 형식 검사Text input rules: case, secret and pattern

코드Code

탭을 골라 코드를 복사하세요. 위 화면은 이 코드를 그대로 실행한 결과예요.Pick a tab and copy the code. The preview above runs exactly this code.

이 화면은 새 모양 옵션을 켠 모양이에요: density: 'spacious', iconStyle: 'soft', motion: true (코드에 없으면 기본 모양으로 보여요)This preview has the new-look options on: density: 'spacious', iconStyle: 'soft', motion: true (without them in your code you get the default look)

빌드 도구(Vite 등)를 쓰는 프로젝트용 모듈 파일이에요. 설치 없이 바로 돌려 보려면 HTML 탭이나 직접 해보기를 쓰세요.

버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.

index.html
<div id="app">
  <div class="toolbar">
    <button type="button" id="editCode">코드 편집</button>
    <button type="button" id="editPassword">비밀번호 편집</button>
  </div>
  <div id="grid" style="height: 250px"></div>
</div>
<script type="module" src="./main.js"></script>
main.js
import { createGrid } from 'maxgrid';
import { actions, options, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);
document.getElementById('editCode').addEventListener('click', () => actions.editCode(api));
document.getElementById('editPassword').addEventListener('click', () => actions.editPassword(api));
example.js
const rows = [
  { id: 1, name: '무선 마우스', code: 'AB123', user: 'minji', password: 'hunter2' },
  { id: 2, name: '기계식 키보드', code: 'KB204', user: 'junho', password: 'letmein' },
  { id: 3, name: '웹캠', code: 'WC001', user: 'seoyeon', password: 'correct-horse' },
];
// 글자 입력 규칙은 editorParams에 써요. 규칙을 주지 않은 칸은 지금까지와 똑같아요.
// 코드: 대문자로 바꾸고 형식(영문 2자 + 숫자 3자)을 입력하는 동안에도 안내해요(patternMessage가 있을 때).
// 사용자: 소문자로 바꿔요. 비밀번호: 입력을 가려요(복사를 막지는 않아요).
export const options = {
  columns: [
    { id: 'name', title: '상품', width: 150 },
    {
      id: 'code',
      title: '코드',
      width: 110,
      editable: true,
      editorParams: {
        case: 'upper',
        pattern: '[A-Z]{2}\\d{3}',
        patternMessage: '영문 2자와 숫자 3자(예: AB123)',
        maxLength: 5,
      },
    },
    {
      id: 'user',
      title: '사용자',
      width: 110,
      editable: true,
      editorParams: { case: 'lower' },
    },
    {
      id: 'password',
      title: '비밀번호',
      width: 130,
      editable: true,
      editorParams: { secret: true },
      formatValue: (value) => '•'.repeat(String(value ?? '').length),
    },
  ],
  rows,
  rowKey: 'id',
};
export function setup(api, log) {
  log({
    ko: '"코드"를 더블클릭하고 ab12를 써 보세요. 대문자로 바뀌고 형식 안내가 떠요.',
    en: 'Double-click "Code" and type ab12. It turns upper-case and the format hint appears.',
  });
  const offs = [
    api.on('editCommitted', (e) => log(e.columnId === 'password' ? `${e.columnId}: ••••` : `${e.columnId}: ${String(e.value)}`)),
    api.on('validationFailed', (e) => log(`${e.columnId}: ${e.message}`)),
  ];
  return () => offs.forEach((off) => off());
}
export const actions = {
  editCode: (api) => api.startEdit(1, 'code'),
  editPassword: (api) => api.startEdit(1, 'password'),
};

Module files for a project with a bundler such as Vite. To run it with no setup, use the HTML tab or Try it.

When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.

index.html
<div id="app">
  <div class="toolbar">
    <button type="button" id="editCode">Edit code</button>
    <button type="button" id="editPassword">Edit password</button>
  </div>
  <div id="grid" style="height: 250px"></div>
</div>
<script type="module" src="./main.js"></script>
main.js
import { createGrid } from 'maxgrid';
import { actions, options, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);
document.getElementById('editCode').addEventListener('click', () => actions.editCode(api));
document.getElementById('editPassword').addEventListener('click', () => actions.editPassword(api));
example.js
const rows = [
  { id: 1, name: 'Wireless mouse', code: 'AB123', user: 'minji', password: 'hunter2' },
  { id: 2, name: 'Mechanical keyboard', code: 'KB204', user: 'junho', password: 'letmein' },
  { id: 3, name: 'Webcam', code: 'WC001', user: 'seoyeon', password: 'correct-horse' },
];
// Text input rules go in editorParams. A cell with no rules behaves as before.
// Code: converted to upper case, and the format (2 letters + 3 digits) is shown while typing (when patternMessage is set).
// User: converted to lower case. Password: the input is masked (copying is not blocked).
export const options = {
  columns: [
    { id: 'name', title: 'Product', width: 170 },
    {
      id: 'code',
      title: 'Code',
      width: 110,
      editable: true,
      editorParams: {
        case: 'upper',
        pattern: '[A-Z]{2}\\d{3}',
        patternMessage: '2 letters and 3 digits (e.g. AB123)',
        maxLength: 5,
      },
    },
    {
      id: 'user',
      title: 'User',
      width: 110,
      editable: true,
      editorParams: { case: 'lower' },
    },
    {
      id: 'password',
      title: 'Password',
      width: 130,
      editable: true,
      editorParams: { secret: true },
      formatValue: (value) => '•'.repeat(String(value ?? '').length),
    },
  ],
  rows,
  rowKey: 'id',
};
export function setup(api, log) {
  log('Double-click "Code" and type ab12. It turns upper-case and the format hint appears.');
  const offs = [
    api.on('editCommitted', (e) => log(e.columnId === 'password' ? `${e.columnId}: ••••` : `${e.columnId}: ${String(e.value)}`)),
    api.on('validationFailed', (e) => log(`${e.columnId}: ${e.message}`)),
  ];
  return () => offs.forEach((off) => off());
}
export const actions = {
  editCode: (api) => api.startEdit(1, 'code'),
  editPassword: (api) => api.startEdit(1, 'password'),
};